PHP Sessions

last edited on: 08-Feb-2018; tagged: php,sessions


A session is a way to store information temporarily to be used across multiple pages. Session stores data on the server rather than user's computer. A session creates a file in a temporary directory on the server. In this file registered session variables and their values are stored. This data will be available to all pages on the site during that visit. In the php.ini file we can define the file location using called session.save_path. PHP Sessions work by creating a unique identification (UID) number for each visitor. And storing variables based on this ID. 

Start/Resume a PHP Session:
For storing any information in session, we must first start up the session. To start a new session we can use the PHP session_start() function. It will create a new session and generate a unique session ID for the user or resume an existing session. Example:

<?php
// starting session in PHP
session_start();
?>

We must call the session_start() function at the beginning of the page. The session_start() function first checks if a session is already exists by looking for the presence of a session ID. If it finds a session ID, it sets up the session variables and if doesn't found any then it starts a new session by creating a new session ID.

Storing and Accessing PHP Session Data:
The $_SESSION[] superglobal array is used to store data. The stored data can be accessed till the session exists. Example:

<?php
// starting session in PHP
session_start();
 // storing session data
$_SESSION["firstname"] = "Emma";
$_SESSION["lastname"] = "Megan";
?>

All session variable values are stored in the global $_SESSION variable. To access the session data we can just retrieve by the key. Example:

<?php
// starting session in PHP
session_start();
 // retrieving session data
echo “Hello ” . $_SESSION["firstname"] .” ”. $_SESSION["lastname"];
?>

Modify a PHP Session Variable:
To change/modify a session variable, we just need just overwrite it as below:

<?php
session_start();
// to change/modify a session variable, we just need to overwrite it 
$_SESSION["firstname"] = "Megan";
$_SESSION["lastname"] = "Holman";
?>

Removing data from PHP Session:
To delete/remove a single value from a session variable we:

<?php
// starting session in PHP
session_start();
// Removing session data
if(isset($_SESSION["lastname"])){
    unset($_SESSION["lastname"]);
}
?>

To delete/remove an entire session completely the session_destroy() function is used. This function does not take any argument and a single call destroys all the session data.

Turning on Auto Session:
If we want to start a session when an user visit a site we can set session.auto_start variable to 1 in php.ini file. 

PHP Session Timeout:
Every PHP session has a timeout value which is measured in seconds. This means how long a session should remain alive in the absence of any user activity. We can adjust this timeout duration by changing the value of session. gc_maxlifetime variable in in php.ini file.
 

Related

PHP Magic Constants

tagged: php

PHP Cookies

tagged: php,cookies