Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Easy session example in PHP - keeping each customers data apart

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
H112 - PHP - Further Web Page and Network Handling
  [4070] Passing variable between PHP pages - hidden fields, cookies and sessions - (2013-04-26)
  [3918] Multiple page web applications - maintaining state - PHP - (2012-11-10)
  [3568] Telling which ServerAlias your visitor used - useful during merging domains - (2012-01-04)
  [3432] 3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11)
  [3036] Sending out an email containing HTML from within a PHP page - (2010-11-07)
  [2918] Downloading a report from the web for further local analysis - (2010-08-13)
  [2729] Uploading a document or image to its own URL via a browser - (2010-04-18)
  [2679] How to build a test harness into your PHP - (2010-03-16)
  [2632] Shipping a test harness with your class in PHP - (2010-02-12)
  [1549] http, https and ajp - comparison and choice - (2008-02-22)
  [1518] Downloading data for use in Excel (from PHP / MySQL) - (2008-01-25)
  [1515] Keeping staff up to date on hotel room status - (2008-01-22)
  [1505] Script to present commonly used images - PHP - (2008-01-13)
  [1496] PHP / Web 2 logging - (2008-01-06)
  [1495] Single login and single threaded models - Java and PHP - (2008-01-04)
  [1485] Copyright and theft of images, bandwidth and members. - (2007-12-26)
  [1379] Simple page password protection - PHP - (2007-10-04)
  [1355] .php or .html extension? Morally Static Pages - (2007-09-17)
  [1210] PHP header() function - uses and new restrictions - (2007-05-30)
  [1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
  [1183] Improving searches - from OR to AND? - (2007-05-11)
  [1114] PHP Image upload script - (2007-03-21)
  [1009] Passing GET parameters through Apache mod_rewrite - (2006-12-27)
  [936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
  [904] Of course I'll tell you by email - (2006-10-25)
  [847] Image maps for navigation - a straightforward example - (2006-08-28)
  [789] Hot answers in PHP - (2006-07-02)
  [767] Finding the language preference of a web site visitor - (2006-06-18)
  [675] Adding PHP tags to an old cgi program - (2006-04-08)
  [603] PHP - setting sort order with an associative array - (2006-02-13)
  [565] Using PHP to output images, XML, Style sheets, etc - (2006-01-15)
  [542] Morning image, afternoon image - (2005-12-26)
  [537] Daily Image Santafied - (2005-12-22)
  [484] Setting the file name for a downloaded document - (2005-11-03)
  [451] Accessing a page via POST from within a PHP script - (2005-09-26)
  [443] Server side scripting of styles to suit the browser - (2005-09-12)
  [425] Caching an XML feed - (2005-08-26)
  [410] Reading a news or blog feed (RSS) in your PHP page - (2005-08-12)
  [376] What brings people to my web site? - (2005-07-13)
  [372] Time calculation in PHP - (2005-07-08)
  [356] Sudoku helper or sudoku cheat - (2005-06-23)
  [345] Spotting a denial of service attack - (2005-06-12)
  [314] What language is this written in? - (2005-05-17)
  [220] When to use Frames - (2005-02-19)

H301 - PHP - Sticky fields and session
  [3820] PHP sessions - a best practice teaching example - (2012-07-27)
  [2738] What is all this SESSION stuff about? (PHP) - (2010-04-25)
  [2416] Automating access to a page obscured behind a holding page - (2009-09-23)
  [1911] Remember Me - PHP - (2008-11-28)
  [1766] Diagrams to show you how - Tomcat, Java, PHP - (2008-08-22)
  [1739] Bath, Snake or Nag? - (2008-08-06)


Back to
Separating program and artwork in PHP - easier maintainance, and better for the user
Previous and next
or
Horse's mouth home
Forward to
I loves Melksham
Some other Articles
Looking for hotel rooms in Melksham over Christmas? We still have some availability
Some different pictures from Melksham
What order are operations performed in, in a Perl expression?
I loves Melksham
Easy session example in PHP - keeping each customers data apart
Separating program and artwork in PHP - easier maintainance, and better for the user
Santa Train - another successful trip - 4th December 2011
Letting the foster parents know ... too little or too much?
UK Mapping Data - and more to come - under government Open Data measures
Melksham, Luxury Hotel Rooms, Christmas.
4318 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87 at 50 posts per page


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.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3540_Eas ... apart.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat