Previous   -   Index   -   Next  >


Cookie Trolley


HTTP is a stateless protocol.

The client sends a request to a server, the server responds to the client and the transaction is complete. Subsequent requests to the server are not affected by previous requests. There is no transaction history defined in the protocol, no memory of past activity.

But there are applications which would benefit from a persistent transaction history. The concept of a session in which a user interacts with a web site over a number of web pages is now comonplace,

Three methods can be used to store state information:

Quick questions:

Answers to the above.

Shopping Trolley example using cookies

shopping trolley
<?php
error_reporting(E_ERROR); //only report errors
if ($_GET['clear']) {
   setcookie('Trolley','',time()-3600);
} elseif ($_GET['choice']) {
   $trolley = $_COOKIE['Trolley'] . '<li>' . $_GET['choice'] . '</li>';
   setcookie('Trolley',$trolley);
}
?>
<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/tr/xhtml1/DTD/xhtml11.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb">
<head><title>PHP cookie example</title>
</head><body>
<h1>Shopping trolley</h1>
<h2>PHP cookie version</h2>
<form action="cookieTrolley.php" method="get" >
<p>Choose an item:</p>
<p>
<input type="radio" name="choice" value="camera"/> camera<br />
<input type="radio" name="choice" value="snorkel"/> snorkel<br />
<input type="radio" name="choice" value="octopus"/> octopus<br />
<input type="radio" name="choice" value="cuddly toy"/> cuddly toy<br /><br />
<input type="submit" name="submit" value="Add to the trolley"/>
<input type="submit" name="clear" value="Empty the trolley"/>
</p>
</form>
<?php
if ((!$_GET['choice'] && !$_COOKIE['Trolley']) || $_GET['clear']) {
   echo '<p>Trolley currently empty</p>';
} else {
   echo '<p>Trolley contains:</p><ul>';
   if ($_GET['choice']) {
      echo $trolley;
   } else {
      echo $_COOKIE['Trolley'];
   }
   echo '</ul>';
}
?>
<p><a href="cookieTrolley.html">Return to the PHP notes</a></p>
</body></html>

Things to notice about the Cookie Trolley:

Form action

There is only one PHP file that creates the form and responds to the form submission

<form method="get" action="cookieTrolley.php">

Note that $_SERVER['PHP_SELF'] or $_SERVER['HTTP_REFERER'] could be used in place of cookieTrolley.php.

Accessing form fields:

The radio buttons in this example, appear as $_GET['choice'] in the php script.

<input type="radio" name="choice" value="camera">
...
elseif (!$_GET['choice'])

Similarly the cookie value and the button pressed are available.

if ((!$_GET['choice'] && !$_COOKIE['Trolley'] || $_GET['clear'])

Handling the cookie

Cookies are sent in the HTTP response header and therefore must be set before any other output.
Here the current choice is appended to the current contents of the cookie and the contents of the cookie are then overwritten using setcookie().

$trolley = $_COOKIE['Trolley'] . '<li>' . $_GET['choice'] . '</li>';
setcookie('Trolley',$trolley);

The cookie is cleared by setting an expiry date in the past.
Clearing the cookie contents does not remove the cookie!

setcookie('Trolley','', time()-3600);

Suppression of messages

error_reporting(E_ERROR); //only report errors

See what happens if error reporting is not suppressed.

error_reporting(E_ALL); // show all messages

Good for testing but perhaps not so good in a live site.

As the cookie is sent in the header you must avoid sending output before the cookie is set - otherwise it breaks.

These warnings really should be prevented by using isSet() to test for data before it is used.
Source code for cookieTrolleyNoErr.php.

Quick Questions

Previous   -   Index   -   Next  >

best viewed using Mozilla browsers
© k.mcmanus 2004
Valid XHTML 1.! . Valid CSS . WCAG priority 3 approved