Your shopping cart is empty!
Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site. A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.
The location of the temporary file is determined by a setting in the php.ini file called session.save_path. Bore using any session variable make sure you have setup this path. When a session is started following things happen:
When a PHP script wants to retrieve the value from a session variable, PHP automatically gets the unique session identifier string from the PHPSESSID cookie and then looks in its temporary directory for the file bearing that name and a validation can be done by comparing both values. A session ends when the user loses the browser or after leaving the site, the server will terminate the session after a predetermined period of time, commonly 30 minutes duration.
A PHP session is easily started by making a call to the session_start() function.This function first checks if a session is already started and if none is started then it starts one. It is recommended to put the call to session_start() at the beginning of the page.
Session variables are stored in associative array called $_SESSION. These variables can be accessed during lifetime of a session. The following example starts a session then register a variable called counter that is incremented each time the page is visited during the session. Make use of isset() function to check if session variable is already set or not.
if( isset( $_SESSION['counter'] ) )
$_SESSION['counter'] += 1;
$_SESSION['counter'] = 1;
$msg = "You have visited this page ". $_SESSION['counter'];
$msg .= "in this session.";
Session Functions - Various sessions functions are
SessionHandler — The SessionHandler class
SessionHandlerInterface — The SessionHandlerInterface class