State information can be stored in the web page by either using hidden form fields or by URL re-writing.
In this example POST protocols are used and so the trolley cannot be seen in the URL.
WIth GET protocols this example becomes remarkably similar to URL re-writing.
<?php error_reporting(E_ERROR); 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 hidden form field example</title> </head><body> <h1>Shopping trolley</h1> <h2>PHP hidden form field version</h2> <form action="hiddenFormFieldTrolley.php" method="post" > <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"/> <?php if ( $_POST['submit'] ) { if ( $_POST['choice'] && !$_POST['trolley'] ) { $trolley = '<li>' . $_POST['choice'] . '</li>'; } else if ( !$_POST['choice'] && $_POST['trolley'] ) { $trolley = $_POST['trolley']; } else if ( $_POST['choice'] && $_POST['trolley'] ) { $trolley = $_POST['trolley'] . '<li>' . $_POST['choice'] . '</li>'; } if ( $trolley ) echo '<input type="hidden" name="trolley" value="' . $trolley . "\"/>\n"; } ?> </p> </form> <?php if ( $trolley ) { echo "<p>Trolley contains:</p><ul>$trolley</ul>"; } else { echo '<p>Trolley currently empty</p>'; } ?> <p><a href="hiddenFormFieldTrolley.html">Return to the PHP notes</a></p> </body></html>
The trolley contents are concealed in the form as a single input type hidden.
<input type="hidden" name="trolley" value="<li>camera</li><li>octopus</li>"/>
This example does not use
header("Cache-control: private");
What happens if the user clicks on the browser back button?
Try this in various browsers.
How do you explain the difference in back button behaviour between these three trolleys?
What happens if the user clicks on the browser reload button?
What would happen if Cache-control: private were used?
How would this be different if the GET method were used instead of POST?
How would you re-write this aplication to use URL re-writing instead of a hidden form field?