menu_banner (2K)

Appendix G: Maintaining state with sessions

The HTTP protocol used by the internet is by nature a stateless protocol, which means that after an HTTP request is received and a response is generated the process which generated that response is released. When a subsequent request is received from the same client there is no memory of any previous request, so there is no 'state' maintained between requests. This is because a child process generated by the web server is not tied to any particular client, so after dealing with a request from one client it may deal with many requests from many other clients. A subsequent request from the same client may then be given to a different process, so any 'state' maintained by the previous process would be inaccessible.

The PHP language has a standard method to deal with this called sessions whose workings can be summarised as follows:

The $_SESSION superglobal is an associative array, which means that it can hold any number of name=value pairs. It is also possible for this to be a multi-dimensional array by specifying value as an array instead of a single value. By using this feature I can maintain a different set of variables for each script in a $script_vars array, and by writing them to the $_SESSION array as <script_id>=$script_vars I can keep the session data for each script totally separate from other scripts within the same session.

More information on this topic can be found in Working with a stateless protocol.


http://www.tonymarston.net
http://www.radicore.org

counter