Maintaining State
HTTP is a stateless protocol, which means that once a web server completes a client’s request for a web page, the connection between the two goes away. In other words, there is no way for a server to recognize that a sequence of requests all originate from the same client.
State is useful, though. You can’t build a shopping-cart application, for example, if you can’t keep track of a sequence of requests from a single user. You need to know when a user puts an item in his cart, when he adds items, when he removes them, and what’s in the cart when he decides to check out.
To get around the Web’s lack of state, programmers have come
up with many tricks to keep track of state information between requests
(also known as session tracking). One such technique
is to use hidden form fields to pass around information. PHP treats hidden
form fields just like normal form fields, so the values are available in
the $_GET
and $_POST
arrays. Using hidden form fields, you can
pass around the entire contents of a shopping cart. However, a more common
technique is to assign each user a unique identifier and pass the ID
around using a single hidden form field. While hidden form fields work in
all browsers, they work only for a sequence of dynamically generated
forms, so they aren’t as generally useful as some other techniques.
Another technique is URL rewriting, where every local URL on which the user might click is dynamically modified to include extra information. This extra information ...
Get Programming PHP, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.