PHP Session: How to Start, Get, Modify, and Destroy PHP Sessions?

Here we are going to see How to start PHP Session, How to get, Modify and Destroy sessions in PHP.

What is a PHP Session?

Using PHP Session a website store and pass information from one page to another.

A Session continues until the user quit the website and unlike cookies, the session isn’t stored on the user’s computer but on the server.

This is a way to store information in variables.

The session will create a file where all of the registered session information will be stored.

We can define the location of that file inside our php.ini file.

Both Cookies and Session store user information but there are some differences between them.

For example, cookies are stored on the local computer of the user which holds user information on the other hand sessions are stored on the server-side.

Here are some of the other major differences between cookies and Session-

  • Sessions are stored on Server but Cookies are stored on client machine.
  • Session are saved in Binery or encrypted form and only can be decoded by server. Cookies are stored on client side and it is not secure as compare to session.
  • Session is over when user quits the browser. Cookies expiry date is set by webmaster.
  • For session there is no limit how much data it can store but there is limit for cookies.

How to Start a Session in PHP?

In PHP we start a session with a function session_start() and using PHP Global variable $_SESSION we can set session variables.

If we want to start a session we put this function before anything on the page even before the HTML code.

Also Read: PHP Cookies: What is, How to Create, Modify, Retrieve & Delete?

Using $_SESSION like array we can pass multiple values in this variable which can access across another page.

Here is an example of how to create the session and pass up value in session variables-

 <?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["name"] = "Rahul";
$_SESSION["country"] = "India";
echo "Session variables are set.";
?>

</body>
</html> 

Get PHP Session Variable

In the previous example we learned how to create a session and set session variables but what about accessing those variables.

We can create another page to retrieve and show session information to users.

Session variables are not passed automatically to another page but to pass them up we have to put the session_start() function at the beginning of each page where we want to access them.

Here is how we can retrieve session information in PHP:

 <?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables selectively
echo "You name is " . $_SESSION["name"] . ".<br>";
echo "Your country is " . $_SESSION["country"] . ".";
    

// Print all session information with single command
print_r($_SESSION);
?>

</body>
</html> 

Modify PHP Session Variable

We can not only set and get the session variable but also can modify it.

To modify session variables in PHP we don’t have to use any other function.

Instead, we can just overwrite a session variable whenever we want to change its value.

For example, if our session variable is something like this:

$_SESSION["email"] = "[email protected]";

You can modify it by overwriting its value:

$_SESSION["email"] = "[email protected]";

How to Destroy Session in PHP

Once we achieved what we want with a session there is nothing left but destroying it.

To remove a Session, first, we remove all global session variables and then destroy them.

To unset all session variables we use session_unset() .

And to finally destroy the session we use session_destroy() function.

Here is the complete example of how to destroy a session in PHP-

<?php
session_start();
?>

<!DOCTYPE html>
<html>
<body>

<?php
// Remove Session variables
session_unset();
 
// destroy the session
session_destroy();
?>

</body>
</html>