Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.

Similar presentations


Presentation on theme: "Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function."— Presentation transcript:

1 Cookies, Sessions

2 Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function. The require() function is used to create functions, headers, footers, or elements that will be reused on multiple pages.

3 How to create variables storing values across php scripts’ calls? Client-server connection is not permanent => Cannot be saved in program memory There are many clients connecting simultaneously => Cannot be saved in file (you cannot identify clients as well sometimes)......

4 Different mechanisms of the same solution Cookies Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. Sessions Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.

5 What is a Cookie? A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests for a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

6 How to Create a Cookie The setcookie() function is used to create cookies. Note: The setcookie() function must appear BEFORE the tag. setcookie(name, [value], [expire], [path], [domain], [secure]); This sets a cookie named "uname" - that expires after ten hours. …

7 How to Retrieve a Cookie Value To access a cookie you just refer to the cookie name as a variable or use $_COOKIE array Tip: Use the isset() function to find out if a cookie has been set. <?php if (isset($uname)) echo "Welcome ". $uname. "! "; else echo "You are not logged in! "; ?>

8 How to Delete a Cookie It will expire or Cookies must be deleted with the same parameters as they were set with. If the value argument is an empty string (""), and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client.

9 What is a Session? The session support allows you to register arbitrary numbers of variables to be preserved across requests. A visitor accessing your web site is assigned an unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

10 How to Create a Session The session_start () function is used to create cookies. <?php session_start(); ?>

11 How to Retrieve a Session Value Register Session variable session_register('var1','var2',...); // will also create a session PS:Session variable will be created on using even if you will not register it! Use it <?php session_start(); if (!isset($_SESSION['count'])) $_SESSION['count'] = 0; else $_SESSION['count']++; ?>

12 How to Delete a Session Value session_unregister(´varname´); How to destroy a session: session_destroy()

13 Using Cookies Cookies are small pieces of data that a server sends to a browser for storage. When a browser contacts a server, it sends along any cookies for that server under the variable $_COOKIES. Similarly, a server can set one or more cookies on the browser for retrieval at a later time.

14 The first part of program session-cookies.php illustrates the typical use of cookies, with these lines: $today = date('l, F j, Y'); $timestamp = date('g:i A'); if (strcmp($_COOKIE[LAST_VISIT], "") == 0) { $lasttime = ""; } else { $lasttime = $_COOKIE[LAST_VISIT]; } $LAST_VISIT = $today. " at ". $timestamp; // set last_visit cookie with date/time, with expiration for 2 full weeks setcookie ("LAST_VISIT", $LAST_VISIT, time() + 3600*24*14); if ($_COOKIE[VISIT_NUMBER] == 0) { $visitcount = 0; } else { $visitcount = $_COOKIE[VISIT_NUMBER]; } // set visit_number cookie with count, with expiration for 2 full weeks setcookie ("VISIT_NUMBER",1 + $visitcount, time() + 3600*24*14);

15 additional notes: Here are a few additional notes: Cookies are sent with Web page headers, so any setting of cookies must take place BEFORE the DOCTYPE line in an HTML/PHP script. PHP function setcookie specifies a cookie ID, a value, and a length of time for which the cookie will be kept by the browser. PHP variable $_COOKIE is an associative array that maintains the list of cookies set previously.

16 Check if your browser is usually set to accept cookies. If you use the Mozilla browser, this information can be found by looking under "Preferences" in the "Edit" menu, and then going to "Privacy & Security" and "Cookies". If you use the Iceweasel browser, this information can be found by looking under "Preferences" in the "Edit" menu, and then going to the "Privacy" tab. If you use Internet Explorer under Windows, this information can be found by looking under select "Internet Options" from the "Tools" menu, then look under "General" and "Settings" in the "Temporary Internet Files" section. If you use Internet Explorer on a Macintosh, this information can be found by looking under "Preferences" under the "Explorer" menu, and then looking under "Cookies" in the "Receiving Files" section.

17 Session Variables Effectively, session variables are cookies that remain active only while the browser is actively interacting with the server. When time elapses, or when you close your browser, the session variables disappear. (If cookies are not allowed by a user, then information for sessions may be placed in a query string at the end of a URL.) The following lines from session-cookies- 2.php illustrate typically processing of session variables.

18 The following lines illustrate typically processing of session variables. // check if person has logged in previously session_start(); $processingOK = "not yet"; $firstLogin = "no"; if (isset ($_SESSION['authorized'])) { // user already logged in $processingOK = $_SESSION['authorized']; } else { // user not logged in, so check password $password = trim($_POST['password']); if ($password == 'Test') { // correct password given $processingOK = 'ok'; $_SESSION['authorized'] = 'ok'; $firstLogin="yes"; } else { // invalid password }

19 Here are some notes regarding session variables: A script uses session_start() to initialize and register any session variables. As with cookies, session variables are sent with Web page headers, so any setting of session information must take place before the DOCTYPE tag. PHP variable $_SESSION is an associative array that maintains the list of session variables set previously. PHP function isset determines whether a specific $_SESSION field has a designated value. PHP function unset removes a session value that was sent previously, and session_unset() removes all session values.

20 Exercise Write a program called Web page session- cookies.php that tries to save a cookie to keep track of whether or not you have visited this page previously.


Download ppt "Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function."

Similar presentations


Ads by Google