In this post I’ll write about cookies in php that are very useful for retaining information through repeated visits by a browser.. Here , I’ll cover PHP’s session-tracking system in details.
A cookie is basically a string that contains several fields. A server can send one or more cookies to a browser in the headers of a response. Some of the cookie’s fields indicate the pages for which the browser should send the cookie as part of the request. The value field of the cookie is the payloadservers can store any data they like there (within limits), such as a unique code identifying the user, preferences, etc.
setcookie( ) function
setcookie(name [, value [, expire [, path [, domain [, secure ]]]]]);
A unique name for a particular cookie. You can have multiple cookies with different names and attributes. The name must not contain whitespace or semicolons.
The arbitrary string value attached to this cookie. The original Netscape specification limited the total size of a cookie (including name, expiration date, and other information) to 4 KB, so while there’s no specific limit on the size of a cookie value, it probably can’t be much larger than 3.5 KB.
The expiration date for this cookie. If no expiration date is specified, the browser saves the cookie in memory and not on disk. When the browser exits, the cookie disappears. The expiration date is specified as the number of seconds since midnight, January 1, 1970, GMT. For example, pass time( )+60*60*2 to expire the cookie in two hours’ time.
The browser will return the cookie only for URLs below this path. The default is the directory in which the current page resides. For example, if /store/front/cart.php sets a cookie and doesn’t specify a path, the cookie will be sent back to the server for all pages whose URL path starts with /store/front/.
The browser will return the cookie only for URLs within this domain. The default is the server hostname.
The browser will transmit the cookie only over https connections. The default is false, meaning that it’s okay to send the cookie over insecure connections.
How To Access To Cookie
you can access that cookie through the $_COOKIE array. The key is the cookie name, and the value is the cookie’s value field. For instance, the following code at the top of a page keeps track of the number of times the page has been accessed by this client:
$page_accesses = $_COOKIE['accesses'];
HTML page that gives a range of options for background and foreground colors:
<html> <head><title>Set Your Preferences</title></head> <body> <form action="prefs.php" method="post"> Background: <select name="background"> <option value="black">Black</option> <option value="white">White</option> <option value="red">Red</option> <option value="blue">Blue</option> </select><br /> Foreground: <select name="foreground"> <option value="black">Black</option> <option value="white">White</option> <option value="red">Red</option> <option value="blue">Blue</option> </select><p /> <input type="submit" value="Change Preferences"> </form> </body> </html>
The form in Example 1-1 submits to the PHP script prefs.php, which is shown in Example 1-2. This script sets cookies for the color preferences specified in the form. Note that the calls to setcookie( ) are made before the HTML page is started.
<?php $colors = array('black' => '#000000', 'white' => '#ffffff', 'red' => '#ff0000', 'blue' => '#0000ff'); $bg_name = $_POST['background']; $fg_name = $_POST['foreground']; setcookie('bg', $colors[$bg_name]); setcookie('fg', $colors[$fg_name]); ?> <html> <head><title>Preferences Set</title></head> <body> Thank you. Your preferences have been changed to:<br /> Background: <?= $bg_name ?><br /> Foreground: <?= $fg_name ?><br /> Click <a href="prefs-demo.php">here</a> to see the preferences in action. </body> </html>
The page created by Example 1-2 contains a link to another page, shown in Example 1-3, that uses the color preferences by accessing the $_COOKIE array.
$bg = $_COOKIE['bg'];
$fg = $_COOKIE['fg'];
<body bgcolor="<?= $bg ?>" text="<?= $fg ?>">
<h1>Welcome to the Store</h1>
We have many fine products for you to view. Please feel free to browse
the aisles and stop an assistant at any time. But remember, you break it
you bought it!<p>
Would you like to <a href="prefs.html">change your preferences?</a>