Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
What is all this SESSION stuff about? (PHP)

If you're booking an airline flight online, you'll be taken through a series of screens to select route, dates, times, passengers, seats, then to enter payment details, and perhaps visa / government information too. It would be impractical to do the whole job on a single page, as you need the intermediate feedback.

How does the booking web site achieve this multi-step process for you, keeping your data from page to page - sometimes for quite a few minutes while you check with the family / work out if you can get the dog to the kennels and still catch the 11:15 flight - and not get you confused with the hundreds of other customers on the same web site at the same time?

This separation of individual users, overlaying a web based / http system which is a "stateless" structure, is commonly achieved using Cookies

• When you arrive at a site, you do NOT have a cookie for that site.

• When the site sends out its first (program) response to you, it includes a cookie in the headers. This is something like "userid=xxyyzp" where the server / site has produced a unique string for the value - xxyyzp in my example. The next user arriving may get "userid=xxyyzq" and so on.

• On each subsequent request to the site, your browser will automatically include the cookie in it headers, and the web site's programs will then identify which particular visitor it is who's returning.

Of course, the string "xxyyzp" doesn't include all the vital data such as a note of where you want to fly to, and on what day, which you enter early on your visit to the site - information which is needed much later in the booking process too - so the server will be programmed to save all these various values to a file (either a regular file, or a record within a database - both schemes work well) at the end of each page, and will read them back in at the start of processing the next page. Sometimes you'll hear this file referred to as you session, at other times as your shopping cart.

From yesterday's PHP course, I have uploaded the source code of an example I wrote [here] which shows how cookies are used to create session files. In the example, we are only saving one piece of data in the file (the number of previous visits), as it's just an illustration of principle.

Sessions of this sort are a VERY common requirement in PHP applications, and my example above needs to be enhanced to deal with issues such as cleaning up completed (expired) sessions, and keeping the session files in another place so that they can't be accessed directly. So PHP has, built in, some special session handling facilities, and a superglobal array called $_SESSION to automate the process for you. There's a complete source code example - the equivalent of the example above - [here]. You'll note that it's much shorter!

When you call session_start, PHP reads in any existing session into the $_SESSION superglobal, or initializes a new session and sends out a cookie (by default called PHPSESSID) to a new arrival. At the end of your script, $_SESSION is automatically saved (disc or database) so that anything it contains is available to you as soon as you have done your session_srat on the next page.

The PHP session functions also tidy up old sessions, send out cookies which expire after a certain time, etc ... i.e. they do all the bookkeeping for you. So my second code sample was shorter ... and also more secure and complete.
(written 2010-04-25, updated 2010-05-14)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H301 - PHP - Sticky fields and session
  [1739] Bath, Snake or Nag? - (2008-08-06)
  [1766] Diagrams to show you how - Tomcat, Java, PHP - (2008-08-22)
  [1911] Remember Me - PHP - (2008-11-28)
  [2416] Automating access to a page obscured behind a holding page - (2009-09-23)
  [3540] Easy session example in PHP - keeping each customers data apart - (2011-12-06)
  [3820] PHP sessions - a best practice teaching example - (2012-07-27)
  [3918] Multiple page web applications - maintaining state - PHP - (2012-11-10)
  [4070] Passing variable between PHP pages - hidden fields, cookies and sessions - (2013-04-26)

A207 - Web Application Deployment - HTTP
  [484] Setting the file name for a downloaded document - (2005-11-03)
  [1378] Etag in http headers - what is it? - (2007-10-03)
  [1503] Web page (http) error status 405 - (2008-01-12)
  [1549] http, https and ajp - comparison and choice - (2008-02-22)
  [2321] Uploading and Downloading files - changing names (Perl and PHP) - (2009-08-04)
  [2596] Http protocol - what does a web server send - (2010-01-24)
  [2918] Downloading a report from the web for further local analysis - (2010-08-13)
  [3432] 3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11)

Back to
Improving your function calls (APIs) - General and PHP
Previous and next
Horse's mouth home
Forward to
Melksham Scouts
Some other Articles
A simple server benchmark script
What is a factory?
Melksham Hustings at George Ward School
Melksham Scouts
What is all this SESSION stuff about? (PHP)
Improving your function calls (APIs) - General and PHP
Perl Course FAQ
Type checking, Java arrays and collections
for and foreach in Java
Travel Troubles and Jesus again
4759 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, 88, 89, 90, 91, 92, 93, 94, 95, 96 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., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2738_Wha ... -PHP-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb