How To Use Sessions in PHP

Session in PHP is used to store data within a browser. It is best for temporarily storing user's login details or website preferences.

img

The main difference between session and cookies is that cookies are stored on user's computer, while session is only limited to the browser.

Once the browser is closed, the session will destroy.

Creating a Session

Session in PHP is created using session_start() function. Session data are stored in a global variable, $_SESSION, which is an array as well. Here is the code for page1.php:

<?php

session_start();

$_SESSION["user"] = "John";
$_SESSION["age"] = 25;

?>

The above code will store user's name and age into the session.

Reading Session Data

We will create another page to test if the session is working fine. Code for page2.php:

<?php

session_start();

echo "Name: ".$_SESSION["user"]."<br>";
echo "Age: ".$_SESSION["age"]."<br>";

?>

Output:

Name: John
Age: 25

Note: You must visit page2.php after you have visited page1.php at least once.

In the code above, we again added session_start() to enable our code to access session data.

Simply using echo, we displayed user and age from $_SESSION variable. This will work on any page as long as the browser window is not closed, and session_start() is present. In case you visit page2.php in a freshly opened browser, the variables will not be recognized. You can utilize isset() function here to redirect users to base page or display a message that the user is not logged in.

Since, $_SESSION is an array, you can also use print_r() function to display all the data it is holding.

<?php

print_r($_SESSION);

?>

Output:

Array
(
    [user] => John
    [age] => 25
)

Changing The Session Values

Just like changing a normal variable in PHP, we can also change the value of $_SESSION index. Here is the code:

<?php

session_start();

$_SESSION["age"] = 30;

echo $_SESSION["age"]; //output: 30

?>

It again needs to use session_start() first. Then, we changed the value of $_SESSION["age"] from 25 to 30. This change will reflect on every page accessed during this browser session.

This is helpful while for instance you have to change user's password, or the color preference. There are many other uses of modifying session variables, which will be apparent when you code your own apps.

Destroying a Session

If you decide to destroy the session completely, you will need this in destroy.php:

<?php

session_start();

session_unset();

session_destroy();

?>

Here again, you start with session_start() to work within the existing session. session_unset() will remove all session variables we created, such as user and age.

Then, we use session_destroy() to delete the session.

The practical example of destroying the session is when you create a logout page. Once the session is destroyed, no page will show session variables. The session will be considered fresh. In case you need to access session variables again, you will need to create and define it again.

Source code

destroy.php

<?php

session_start();

session_unset();

session_destroy();

?>

page1.php

<?php

session_start();

$_SESSION["user"] = "John";
$_SESSION["age"] = 25;

?>

page2.php

<?php

session_start();

echo "Name: ".$_SESSION["user"]."<br>";
echo "Age: ".$_SESSION["age"]."<br>";

print_r($_SESSION);

$_SESSION["age"] = 30;

echo $_SESSION["age"]; //output: 30

?>

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Join our community!

Delete an article

Deleted articles are gone forever. Are you sure?