When you go to a shop, you're not kept waiting outside until the previous customer leaves. The shop will have multiple shopping baskets / trollies, or muliple assistants. And that's the way you want it when you visit a web site too. This means that the web site programmer has to be very careful to keep each visitor's data separate from the other visitors. At a low level, you can use cookies to do this, but there can be quite a bit of coding to do - and of course it's been done many times before, and you won't want to re-invent the wheel. So in PHP you can use sessions which do all the hard work for you, using an underlying cookie level that you need not see.

It works like this:

1. At the start of your PHP page, you call session_start. This initialises the session system for the current page, and loads in any data retained from YOUR previous pages (not the previous pages of other visitors currently on the site!)

2. You use and save values that you want to be retained from page to page in an array called $_SESSION

3. At the end of your PHP script, your PHP automatically, without any extra calls saves teh contents of $_SESSION to a file or database (depending on how your PHP is configured), with a note of the particular user / cookie value it applies to, ready for releoding next time you come to step 1.

There's a new example - showing how simple this can be - [here]. On the first visit to our cafe, the waiter gives the new customer a free beverage which he selects at randonm from a list. On the second and subsequent visit, the waiter gives the customer the same drink, tells him how many times he has had that drink, and charges him for it.

Note: Also built in to the session mechanism is a scheme for tidying up old sessions that people have abandoned. When someone completes a visit / purchase you'll normally use session_destroy but people often walk away from websites without completing an order ...

(written 2011-12-06)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles

This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.