Introduction on PHP sessions
Sessions are used within web applications. The use of session 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.
To begin a session we use session_start() function. And when the session starts its 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.
How to Create Sessions?
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 what is the path of the directory 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.
4.5 (5,731 ratings)
View Course
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:
How to Delete Sessions?
Following is an example for delete session:
Example
Session_destroy() function is used to destroy a session. This function destroys the complete session. To unset a single session variable we can use 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:
How to Destroy Session?
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.
Example #1
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.
Example #2
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:
How to Turn on Auto Session in Php?
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.
Importance of session
Some importance of session are penned below.
- Like $_COOKIE, we have $_SESSION, which is a superglobal variable and stored over the server.
- In case the cookies are not supported by the browser of the user, we can use the session. Each session has a unique id assigned to it as per the user visit on the website.
- In terms of storing data, the session stores more data than a cookie can store.
- The session is used to carry information from one page to another.
- The session can be used to get the count of visitors to the website.
- The session is used for authentication purposes also.
- Session IDs generated is unique.
Conclusion
In this article, it is explained what is a session, 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 is the importance of session and how it is useful for different purposes.
Recommended Articles
This is a guide to Sessions in PHP. Here we discuss the basic concept, how to Create Sessions and Delete Sessions in PHP with the given examples. You may also have a look at the following articles to learn more –