PHP Tutorial: PHP Session

PHP Session

For accessing data accross whole website pages, we use the PHP Session.

Session stores on the server, 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 website pages visit.

The location of the temporary file is determined by a setting in the php.ini file called session.save_path.
PHP session creates unique user id for each browser to recognize the user and avoid conflict between multiple browsers.

Starting a PHP Session

Before we can store any information in session variables, we must first start up the session. To begin a new session, we simply call the PHP session_start() function. It will create a new session and generate a unique session ID for the user.
The PHP code in the example below simply starts a new session.

<?php
// Starting session
session_start();
?>

The session_start() function first checks to see if a session already exists by looking for the presence of a session ID. If it finds one, i.e. if the session is already started, it sets up the session variables and if doesn't, it starts a new session by creating a new session ID.

Storing and Accessing Session Data

We can store all your session data as key-value pairs in the $_SESSION[] superglobal array. The stored data can be accessed during lifetime of a session. Consider the following script, which creates a new session and registers two session variables.

Example:

<?php
// Starting session
session_start();
 
// Storing session data
$_SESSION["firstname"] = "Peter";
$_SESSION["lastname"] = "Parker";
?>

To access the session data we set on our previous example from any other page on the same web domain — simply recreate the session by calling session_start() and then pass the corresponding key to the $_SESSION associative array.

Example:

<?php
// Starting session
session_start();
 
// Accessing session data
echo 'Hi, ' . $_SESSION["firstname"] . ' ' . $_SESSION["lastname"];
?>

Destroying a Session

A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.

Here is the example to unset a single variable −

<?php
   unset($_SESSION['counter']);
?>

However, to destroy a session completely, simply call the session_destroy() function. This function does not need any argument and a single call destroys all the session data.

<?php
// Starting session
session_start();
 
// Destroying session
session_destroy();
?>

Every PHP session has a timeout value — a duration, measured in seconds — which determines how long a session should remain alive in the absence of any user activity. You can adjust this timeout duration by changing the value of session.gc_maxlifetime variable in the PHP configuration file

 

Next Topic