Recipe 7.10. Using Cookies to Remember Visitor Choices


You need to store small bits of information about your web site visitors' preferences and retrieve them on their subsequent visits to your site.


Record the settings as a cookie on a visitor's hard drive using the built-in PHP function setcookie( ), and then access it with PHP's built-in array of cookie variables associated with the current browser session, $_COOKIE.


Browser cookies are an oft-debated and indispensable part of the Web. Privacy advocates rightfully point out that abuse can allow unscrupulous web sites to know much of the personal information and browsing habits of their visitors without their permission. But most of the world's most popular web sitesfrom to Yahoo!would operate much differently without cookies. Whenever a site remembers your login information, or your name and address, it's using cookies to do so.

Cookies are small individual files, or indexed entries in one larger file, that storeon the visitor's own hard drivea bit of information about a particular user for a particular web site. They typically have a name, the data to be stored, the host name of the site that controls the cookie, the file path on the host where the cookie is valid, and an expiration date. Cookies are exclusive to a specific browser on a specific computer. For example, if you have an account with the New York Times web site, you'll need a cookie on both your home and work computer to bypass the login screen and browse the site.

Your site might never have a need to record complex visitor preferences in a cookie. But even a simple use of a cookie can be a valuable way to tailor your visitors' experience with your site, such as choosing a language or plug-in preference. In this basic example, I'll explain how to set a cookie to remember a visitor preference using a choice screen. On our hypothetical home page (see Figure 7-13), a visitor must declare his relationship to the web site ("wholesaler" or "retailer") before proceeding to the site.

Figure 7-13. A visitor must identify himself on this choice page

The code for the page in Figure 7-13 uses a conditional statement to check for a cookie:

 <?  if (isset($_COOKIE["home"])) {   header("Location:".$_ COOKIE["home"]);  } else { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Retailer or Wholesaler?</title> </head> <body> <table width="50%" height="300" align="center" cellpadding="0" cellspacing="0">   <tr align="center" valign="middle">     <td colspan="2"><h3>Are you a retailer or a wholesaler?</h3></td>   </tr>   <tr align="center" valign="middle">     <td width="50%" bgcolor="#0000FF"><a href="who.home.php">Wholesaler</a> </td>     <td bgcolor="#FF0000"><p><a href="ret.home.php">Retailer</a></p>     </td>   </tr> </table> </body> </html> <? } ?> 

At the top of the code, before any of the visible part of the page is sent to the browser, the condition isset($_COOKIE["home"]) checks for the presence of a cookie named home. If the variable has a value, which in this example will be the filename of a wholesaler-or retailer-specific home page, the built-in PHP function header( ) redirects the browser to that page.

On the retailer (ret.home.php) and wholesaler (who.home.php) home pages, two lines of PHP code define the expiration date and set the cookie when the chosen page loads:

 <? $expiry = time()+60*60*24*365; setcookie("home","ret.home.php",$expiry); ?> 

The code defines the $expiry variable as one year from the current time and the setcookie( ) function records the cookie. The function sends three parameters to the browser: the cookie name (home), the value (ret.home.php or who.home.php), and the expiration date. The setcookie() function also can record optional information about the path, domain, and connection type for which the cookie applies. Since those values were left blank in this use of the function, Figure 7-14 shows them set to their default values.

See Also

For more about PHP's cookie-handling functions, see and

Figure 7-14. Information recorded as a cookie

Web Site Cookbook.
Web Site Cookbook: Solutions & Examples for Building and Administering Your Web Site (Cookbooks (OReilly))
ISBN: 0596101090
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Doug Addison

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: