PHP – Sessions

PHP basics – Sessions

Sessions store data on the web server. They are useful in eCommerce to store information about purchasing items whilst the customer is in the store. Sessions can hold more data than cookies. With sessions the only thing that is stored on in the clients browser is a single cookie containing the session ID. this is a long string of digits that uniquely identifies the user for the duration of their visit. In PHP, this cookie is a variable that PHP watches for subsequent page requests and which loads data associated with that session.

By default, PHP sets a cookie that contains the session ID. The browser then sends the cookie along with every request for a page from your website so that PHP can determine which session the ID belongs to . PHP stores session data as temporary files within the web server. PHP can then keep track of the variables and values that have been assigned in each session.

In order for sessions to work properly, check the PHP.INI file. In a development server on your computer, check these settings:

session.save_handler   = files
session.save_path      = "C:\WINDOWS\TEMP"
session.use_cookies    = 1

Make sure the save_path path is set to a place that exists. If you change setting, restart the pc.


Start a session


Use this to look for a session ID. If none are found, you can create a new session. If one is found when this function is called, PHP restores that sessions variables.

This function creates a cookie, therefore this function MUST COME BEFORE ANY PAGE CONTENT (just like SETCOOKIE).

Store a variable in the current session:

$_SESSION['password'] = 'this password';

Remove a variable from the current session:


End current session, delete all variables & clear all values:

$_SESSION = array();



Coming up… Create a shopping cart