How to Set, Get and Unset Session Variables in PHP

In this post, we will see the basics of the session handling (i.n, create, access and destroy the session variable) in PHP.

What is Session

A PHP session is used to store the user data on the server temporarily and make that data accessible across the various pages of an entire website. When you close the browser, the session will be finished. A PHP session creates a temporary file on the server where registered session variables and their values are stored. The location of this temporary file is determined by a setting named session.save_path in the php.ini file.

How to Start a Session

Before starting to store/retrieve the information in the session, you must first start up the session by using the session_start() function. This function will check whether a session is already started or not. If none of the session is started then it will start a new session.

Below the example code to start a new session,

<?php
// Starting Session
session_start();

The important thing is that the session_start() function must be called at the beginning of the script, i.e. before any output generated by your script in the browser. Otherwise, you will get the error like Headers are already sent.

You can also be starting a session automatically without using the session_start() function. For that change the session.auto_start variable to 1 in the php.ini file.

How to Create Session Variables

Once a session is started by using session_start() function, the $_SESSION superglobal array is initialized with the corresponding session information. By default, it will be initialized with a blank array, and you can store more information using a key-value pair.

Below the example code to store the information on the session variable,

<?php
// Starting Session
session_start();
 
// Storing Session Data
$_SESSION["firstname"] = "Thomas";
$_SESSION["lastname"] = "Raj";

These session variables can be accessed during the lifetime of a session. The lifetime of a session is determined by a setting named session.gc_maxlifetime in the php.ini file.

How to Access Session Variables

After storing the session information by using the global $_SESSION variable, you can access it anywhere in the website using the corresponding key. 

Below the example code to get the information from the global $_SESSION variable,

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

How to Destroy a Session

To delete any single session variable, use the PHP unset() function with its corresponding key as follows,

<?php
// Starting Session
session_start();
 
// Delete Specific Session Data by Key
if(isset($_SESSION["lastname"])){
    unset($_SESSION["lastname"]);
}

To remove all the global $_SESSION variables, use the session_unset() function as follows,

<?php
// Starting Session
session_start();
 
// Remove Entire Session
session_unset();

To destroy all the global $_SESSION variables, use session_destroy() functions as follows,

<?php
// Starting Session
session_start();
 
// Destroy Entire Session
session_destroy();

The functions session_unset() and session_destroy() do not require any argument and a single call will be destroyed all the session data. Both of this function seems to delete all variables registered to a session but there is a difference between them. The session_destroy() function will destroy all of the data associated with the current session. But the session_unset() deletes only the variables from the current session and the session will exist.

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *