Maintaining State

The Web Server does not remember your past transactions. If you have a sequence of transactions where the action of the present transaction is dependent on a past transaction then the present transaction must have some "memory" of the past transaction. There must be some mechanism in place to transfer information from the past transaction to the present. This transfer of information can be done in 3 ways:

Hidden Variables

Hidden variables are name-value pairs that are embedded in a form that can be checked by the script processing form data on the server side. As the name imples hidden variables are not displayed when the form is rendered on the client side.

Let us consider the following transaction sequence - an order form is displayed, once you place the order a confirmation page is displayed, when you confirm the order a thank-you page is displayed. A hidden variable page keeps track of which page is to be displayed. Here is the application logic:

if ($_POST["page"] == "confirm")
  confirmPage();
elseif ($_POST["page"] == "order")
  orderPage();
else
  orderForm();
When the script is first called the variable page is not set and hence the function orderForm() is called. After that the confirmPage() function is called and finally the function orderPage(). Here is how the transaction sequence works. This is the actual code.

Cookies

A cookie is a small piece of data in the form of a name-value pair that is sent by a Web server and stored by the browser on the client machine. This information is used by the application running on the server side to customize the web page according to the past history of transactions from that client machine.

When a client browser makes a request for a web page from a server, it first checks to see if there are any cookies present from that server in its cookie folder. If there are any cookies then those cookies are sent to the web server along with the request for the page. The application program on the server side reads the cookies and tailors the web page accordingly. It may reset the cookies according to the present transaction.

A transient cookie is there just for the session. A persistent cookie can exist for a long time (order of years). The application program sets the time to live for the cookie. One way of deleting a cookie is to set its time to live to a time in the past.

A cookie is a name-value pair. In its simplest form to set a transient cookie the function setcookie() is used with the name-value pair as parameters:

setcookie ("some_name", "some_value");
To set a permanent cookie the expiration time must be given. One way is to read the current time and then add an increment to it in seconds. For example to set a cookie that lasts for a year the following command will work:
setcookie ("some_name", "some_value", time()+3600*24*365);

The setcookie() function must be called before any printed output occurs. The cookie must be written as part of the HTTP header.

All cookies from that url are sent back to the server when the browser requests a web page. The associative array $_COOKIE holds all the name value pairs. To read the value for a particular cookie do:

$value = $_COOKIE["some_name"];

State Files

In PHP name-value pairs can be stored in state files on the server side. A session can be started by using the command

session_start();
before making any printed output. A unique session id is generated. The script need not know the id but its value can be retrieved from
$id = session_id();
Session ids can be passed as hidden variables or stored as cookies between transactions. To close a session do
session_destroy();

The name-value pairs are stored in an array called $_SESSION. To register a name-value pair do:

$_SESSION["some_name"] = some_value;
To extract or retrieve the value from a session variable:
$value = $_SESSION["some_name"];

This code uses session variables to generate an online quiz, keep track of the number of questions asked and the score.