Sessions in PHP plays a very important role in handling those variables whose values must be accessed by all the pages.
What is a Session?
A session is a simple and unique way to store data. The data that is stored is then accessible to all the pages of the script. The values of the session variable can be stored in the global variable $_SESSION array. This is an associative array i.e. it stores the values in key-value pairs.
A session handler basically creates a file in a temporary directory on the server where all the previously defined session variables and the values associated with it are stored.
The location of the temporary variable is obtained by a setting in a file called php.ini and the setting is called session.save_path.
Properties and uses of a Session in PHP
- Whenever a session is created, firstly it is given a unique a session id. This session id is then used to retrieve the stored values. The session id is stored in a cookie. A cookie always resides on the user’s computer. Secondly, the session is always created on the server side.
- The session values are automatically deleted when the browser is closed.
- The sessions have a much larger capacity to store data in comparison to cookies.
In PHP, by default, it uses the internal files save handler which is set by session_save_handler. This is responsible for saving the session data on the server at the location specified by the session_save_path configuration directive.
We can also pass values from one page to another page using session. Hence it is much secure and efficient way of storing and passing data.
Creating a Session in PHP
In PHP, you can create or start a session by using the following syntax:
session_start();
This function is pre-defined and does not have any argument. This line of code must be declared before all the HTML tags and the HTML tag itself i.e. it must be specified before the HTML tag. This function first checks whether a session already exists and if it doesn’t find any then it creates the new session.
Refer the following code snippet:
<?php session_start(); $_SESSION["variable"]=10; if(isset($_SESSION["variable"])) {echo "The values of the session variable is "$_SESSION["variable"]; } ?>
Modifying a Session
The values of the existing session variables can be overwritten using the same syntax i.e.
$_SESSION[“variable name”]=value;
Before modifying a session variable one can also check whether the variable has been set before or not. This can be checked by using the isset(). The isset() function returns true if the value is set else it returns false.
Look at the code snippet below:
<?php session_start(); $_SESSION["variable"]=123; ?>
One can see that the value of the variable “variable” has been updated.
Destroying Session Variables
The session_destroy() function is used to destroy the whole PHP session variables. This function does not have any argument. If required, one can also delete a single variable from the global session variable array by using the unset() function.
For deleting a single session variable:
<?php unset($_SESSION["variable"]); ?>
For destroying the whole session:
<?php session_destroy(); ?>
Hope you find this tutorial useful about sessions in PHP.