Home  >  Article  >  Backend Development  >  Sessions in PHP

Sessions in PHP

PHPz
PHPzOriginal
2024-08-29 12:42:28963browse

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 Tests

Start 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.

How to Create Sessions in PHP?

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:

Sessions in PHP

How to Delete Sessions in PHP?

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:

Sessions in PHP

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:

Sessions in PHP

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:

Sessions in PHP

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 the session is penned below.

  • Like $_COOKIE, we have $_SESSION, which is a superglobal variable and stored over the server.
  • If the user’s browser does not support the cookies, 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 are unique.

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Cookie in PHPNext article:Cookie in PHP