Full Transcript

COOKIES AND SESSIONS The need for persistence  Consider these examples  Counting the number of “hits” on a website  i.e. how many times does a client load your web page source  The questionnaire on computing experience  Somehow your.php needs to remember prev...

COOKIES AND SESSIONS The need for persistence  Consider these examples  Counting the number of “hits” on a website  i.e. how many times does a client load your web page source  The questionnaire on computing experience  Somehow your.php needs to remember previous instances of it being requested by a client Persistence  Persistence is the ability of data to outlive the execution of the program that created them.  An obvious way of achieving persistence is to simply save the data in a file Persistence and HTTP  Recall http is a stateless protocol. It remembers nothing about previous transfers.  Two ways to achieve persistence:  PHP cookies  PHP sessions COOKIE S Using Cookies in PHP  A cookie is an item of data that a web server saves to your computer’s hard disk via a web browser.  It can contain almost any alphanumeric information (as long as it’s under 4 KB) and can be retrieved from your computer and returned to the server.  Common uses include session tracking, maintaining data across multiple visits, holding shopping cart contents, storing login details, and more. Cookies  Idea  Script sends a simple name and value to client  Client saves the name and value in the file system  Client returns same name and value every time it connects to same site.  Typical Uses of Cookies  Avoiding username and password “Remember me on this computer”  Personalization  Cookies must be sent before any output from your script. A browser/server request/response dialog with cookies Setting a Cookie Setting a cookie in PHP is a simple matter. As long as no HTML has yet been transferred, you can call the setcookie function, which has the following syntax: SET COOKIE PARAMETERS Parameter Description Example name The name of the cookie. This is the name location that your server will use to access the cookie on subsequent browser requests. value The value of the cookie, or the cookie’s hello contents. This can contain up to 4 KB of alphanumeric text. expire (Optional.) The Unix timestamp of the time() + expiration date. Generally, you will probably 2592000 use time() plus a number of seconds. If not set, the cookie expires when the browser closes. SET COOKIE PARAMETERS Paramet Description Example er path (Optional.) The path of the cookie on the server. / If this is a / (forward slash), the cookie is available over the entire domain, such as www.webserver.com. If it is a subdirectory, the cookie is available only within that subdirectory. The default is the current directory that the cookie is being set in, and this is the setting you will normally use. domain (Optional.) The internet domain of the cookie. If webserver.c this is webserver.com, the cookie is available to all om of webserver.com and its subdomains, such as www.webserver.com and images.webserver.com. If it is images.webserver.com, the cookie is available only to images.webserver.com and its subdomains, such as sub.images.webserver.com, but not, say, to www.webserver.com. secure (Optional.) Whether the cookie must use a FALSE secure connection (https://). If this value is TRUE, Setting up a cookie  So, to create a cookie with the name location and the value USA that is accessible across the entire web server on the current domain, and will be removed from the browser’s cache in seven days, use the following:  setcookie('location', 'USA', time() + 60 * 60 * 24 * 7, '/'); Accessing a Cookie  Reading the value of a cookie is as simple as accessing the $_COOKIE system array. For example, if you wish to see whether the current browser has the cookie called location already stored and, if so, to read its value, use the following: if (isset($_COOKIE['location'])) $location = $_COOKIE['location']; Note: You can read a cookie back only after it has been sent to a web browser. This means that when you issue a cookie, you cannot read it in again until the browser reloads the page (or another with access to the cookie) from your website and passes the cookie back to the server in the process. Destroying a Cookie  To delete a cookie, you must issue it again and set a date in the past. It is important for all parameters in your new setcookie call except the timestamp to be identical to the parameters when the cookie was first issued; otherwise, the deletion will fail.  Therefore, to delete the cookie created earlier, you would use the following:  setcookie('location', 'USA', time() - 2592000, '/'); Expiring Cookies  By default, a cookie expires automatically when the browser exits.  To set the expiration of a cookie to 1 hour  setcookie(“mycookie”, $value, time()+3600);  To expire a cookie immediately  setcookie(“mycookie”, $value,time()-3600); Setting Cookies Sample Codes pageA.php pageB.php Example of Cookies  In this example we will be creating a cookie that stores the user’s last visit to measure how often people return to visit our webpage.  We want to ignore people that take longer that two months to return to the site, so we will set the cookie’s expiration date to two months in the future. Setting Up Cookies for 60 Days Retrieving Cookies

Use Quizgecko on...
Browser
Browser