Home > Article > Backend Development > Sessions in PHP
Sessions are used within web applications. The use of sessions in PHP is to make the data available across different pages of a website. The data or information like name, address, etc., is carried from one page to another user session. This session information is used for authentication purposes. Like the cookies are stored on the clients’ browser, the session data is stored on the server in a temporary directory.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
To begin a session we use session_start() function. And when the session starts, it lasts for 24 minutes by default which is 1440 in seconds. A session is identified by session identifiers or SID, which is a unique number to identify each user.
session_start() is the function used to start a session. If a session already exists, it will use the same session; else, it will create a new session. This function is always called at the beginning of each page. After the function is called, we can start storing values in the session variables, but not before that. PHP stores information of the user in a super global variable $_SESSION.
To know the directory path where sessions are stored, we have a directive called session_save_path in the php.ini file to store the path. Also, the session_id prints the id associated with the current session. It is a unique randomly generated number.
Let us look at the below program.
In this program, when you print this session variable using the print_r($_SESSION), all the set session data is printed. The output is in the form of an associative array of key-value pairs.
In this example, we first start the session using session_start() function, initialize variables and assign it to session variables using _SESSION[‘name’] = $name, print the super global array. We also print the unique session id value also with the session_id() function.
Example
Code:
<?php // example to start session //beginning a session // no arguments are passed to this function session_start(); //initializing variables $name = 'Neha';$age = 28; //storing values in session $_SESSION['name'] = $name; $_SESSION['age'] = $age; // printing session valuesprint_r($_SESSION); //using the session id echo '<br > The session_id is '; echo session_id(); ?>
Output:
Following is an example for delete session:
Session_destroy() function is used to destroy a session. This function destroys the complete session. To unset a single session variable, we can use the unset() function.
In this example, we print the session data first to know what the session holds; then, we destroy the already set session variables using the unset() function. Here we destroy both the set session variables like the name and the age. And after destroying, we print the session again and see that it returns an empty array, meaning the variables have been unset and the session does not exist anymore.
Code:
<?php //example to unset session variables //starting a session session_start(); print_r($_SESSION); //before destroying the session //printing the session unset($_SESSION['name']); unset($_SESSION['age']); echo 'Session destroyed'; //after destroying the session //printing the session print_r($_SESSION); ?>
Output:
In the following example, we are destroying a session.
To destroy the session, we will first check the session value and then destroy the session. and again print the session, which will be an empty array as the session does not exist.
Code:
<?php //example to destroy session //starting a session session_start(); //to completely destroy a session session_destroy(); echo 'Session destroyed'; //after destroying the session //printing the session echo '<br />'; print_r($_SESSION); ?>
Output:
Here, in the program, we see that in the first line, we start the session and initialize the count variable to 0. next, we check whether a session variable is set or not. here we check one condition whether a session name page_views is set; if yes, then increment the count variable value by one, and if not, then initialize the count value to one.
Code:
<?php session_start(); $count = 0; if(!isset($_SESSION['page_views'])) { $_SESSION['page_views'] = 1; $count = $_SESSION['page_views']; } else { $_SESSION['page_views'] = $_SESSION['page_views'] + 1 ; $count = $_SESSION['page_views']; } ?> <html> <head><title>Finding count of page views</title></head> <body> <?php echo '<br>'. 'The count of page views '. $count; ?> </body> </html>
Output:
To turn on auto sessions in PHP, we have to make a change in the configuration file, which is php.ini.
Where php.ini is a default configuration file
Sessions do not start on their own automatically; to make a session work automatically, we have to do the following, but once done, the session is started automatically for all the files and closes after the default time is over.
So in the php.ini file, we will search for
session.auto_start = 0
and set the value to 1 like this
session.auto_start = 1
This will help PHP to start sessions automatically.
Some importance of the session is penned below.
This article explains what a session is, how the session works, how do we create the session, how do we delete a particular session.
Also, it is explained how do we make the sessions work, starting automatically by setting the directive session.auto_start() value to 1.
Then with an example, it was explained how to count how many times the page has been viewed using sessions. Also, it is explained what the importance of the session is and how it is useful for different purposes.
The above is the detailed content of Sessions in PHP. For more information, please follow other related articles on the PHP Chinese website!