PHP Sessions

In our previous post, we have learnt about PHP cookie that stores small information into the client device. In this tutorial, you are going to learn about PHP Sessions and how to use it.

A PHP Session stores the information on the server-side rather than storing information in the computer of the user. Session identifier or SID which is a unique number for every session is used to identify every user in a session-based environment.

This SID is used to link each user to their own posts, emails, etc.


Starting a PHP Session

In order to store information related to the session, you must first start the session. session_start() function is used to start the session in PHP. After the session is started, the session variable can be used to store the user’s session information. This function will create new SID for the user.

Syntax

bool session_start ( void );

Example

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

The session_start() function first checks for the existing SID i.e. if the session has already started. If not found any, this function will start the session and created a new SID for the user and if the SID already exists, it sets up the session variable.

Note: You must have to call session_start() function at the beginning of the page before the <HTML> tag just like PHP Cookies. The SID is randomly generated by the PHP engine.

Storing and Accessing Session data

PHP session data can be stored in key-value pairs using $_SESSION[] superglobal array. The stored data in the array can be accessed during the lifetime of the session. Below is the PHP code to store a session with two variable emp_no and emp_name.

<?php
// Starting session
session_start();

// Storing session data
$_SESSION["emp_no"] = "141";
$_SESSION["emp_name"] = "Sagar Sharma";
?>

In order to access the PHP session’s data created in the above example from another page but in the same session, simply recreate the session by calling the session_start() function and by passing the corresponding key to the $_SESSION associative array.

<?php
// Starting session
session_start();

// Accessing session data
echo 'The Employee ID is: ' . $_SESSION["emp_no"];
echo '<br/>';
echo 'The Name of the Employee is: ' . $_SESSION["emp_name"];
?>

Output

The Employee ID is: 141
The Name of the Employee is: Sagar Sharma

Destroying a session in PHP

To delete only a specific session data, you can use the unset feature along with the corresponding session variable in the $_SESSION associative array.
The PHP code to unset only the “emp_no” session variable from the associative session array:

<?php
//starting session
session_start();

//unsetting session data
if(isset($_SESSION["emp_no"])){
unset($_SESSION["emp_no"]);
}

?>

If you want to destroy the entire session the session_destroy() function can be used to completely destroy a session. The session_destroy() function does not require any kind of argument.

<?php
//starting session
session_start();

//destroying session
session_destroy();

?>

 

Please get connected & share!

Advertisement