http://www.phpro.org/tutorials/Introduction-To-PHP-Sessions.html
Simple Session Example
Session Overview:
Session support is a method to preserve data across user requests.
Visitors accessing a web site are assigned a unique id; that is a
session id. The session id is either stored in a cookie on the user
side (client), or is propagated in a URL.
Using sessions allow data to be stored between requests in the $_SESSION superglobal array. When a visitor accesses your site, PHP will checks whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated.
Sessions can be employed in two methods:
- cookie based (preferred)
- url based (problematic with search engines)
Cookie based: when a session is begun, a cookie is created on the
client's machine, with a unique session ID (i.e., SID). Session variables
are typically stored on a file on the server that matches the unique
session ID.
When a variable is required, the client or browser looks for the file
matching the session ID and retrieves the corresponding variables from
it. A typical session file stored in the default session file
directory could look like this:
sess_fd51ab4d1820aa6ea27a01d439fe9959
This file would contain session information in an array.
Basic Session Functions:
- session_start(): begin new or resume existing session
- session_register(variable): register one or more global variables with current session
- session_id(): set/get session ID
- session_unset(): free memory all session variables
- session_destroy(): destroy all data registered to session
More Session Functions:
http://www.php.net/manual/en/ref.session.php
Session Example:
- Begin session
- Initialize/set session variable
- Use PHP super global $_SESSION to hold value
- Recall value on next page
<?php
/*
//Nothing can be sent to the browser before session_start()
//This means no text, not even a newline or a space.
//This will generate and error!
echo 'This is a bad thing to do';
*/
// begin session
session_start();
// initialize super global $_SESSION (array) variable value of 'test'
$_SESSION['myVariable1']='test';
$_SESSION['myVariable2']='me';
?>
Example:
Process sessions
Notes:
- session_start() must be used on every session page.
- In most cases, it will be the first line of code on each page.
- If a session is not already active, PHP will begin a new session upon
adding a new variable to the super global $_SESSION array.
- Nothing can be sent to browser before session_start() no text, not even a newline or a space.
Session Handling:
http://www.php.net/manual/en/book.session.php