IS 337: Website Administration

Lecture for November 3: Cookies

+
-

Topics

Context

  • HTTP is stateless.
  • A website would like to remember you.
  • Cookie transactions toggle
    • A website gives your browser a cookie.
    • Your browser sends that cookie back to that same website.

Using Cookies in PHP

  • Do the heavy lifting ourselves (server side!) toggle
    • Receiving: parse the HTTP request header:
      Cookie: NAME1=OPAQUE_STRING1; NAME2=OPAQUE_STRING2 ...
      
    • Sending: could write the raw HTTP header line ourselves:
      Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
      
  • Let PHP do the lifting toggle
    • setcookie(name, value, expire, path, domain, secure)
    • $_COOKIE[] and $_REQUEST[] superglobals
  • Clarifications toggle
    • setcookie() must be called before any output is generated
    • expire measured in seconds since the epoch (e.g., time()+60*60*24*30 for 30 days)
    • if expire == 0, cookie expires when browser is closed
    • if name uses "array notation", $_COOKIE[] will process as array

Readings and Resources

Quick Written Assignment for Next Time

Write two PHP functions: one that returns your name and another that prints your name.

Creative © 2007 Calvin College and Jeremy D. Frens.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.