Yogesh Chauhan's Blog

The Complete Basic Explanation of PHP Sessions

in PHP on December 9, 2019

So, what are PHP Sessions?

In simple terms, it's a way to store information in different variables so that we can use all those variables and information on other pages whenever required. 

So, is there any difference between a session and a cookie?

Well, cookie stores information on user's computer or any device. Session stores information in a browser. 

Let's understand by an example.

Whenever you work with any application, you start a session. For example, you opened an application called Google Chrome, you surfed some pages to find out some information and then you closed it after few minutes. This whole state from opening to closing can be considered as a session. 

Whenever you are using any application offline, for example, a Word or a PDF file, the computer stores info and it's really easy for the computer to do so and recognize you. But when you browse any pages on the internet and try to make a connection to any server, it becomes hard to recognize who you are. The reason for that is that the HTTP address doesn't maintain the state. 

HTTP address doesn't maintain the state. 

There is a solution. Session variables. We can Session variables to store user information and we can use it later on… on many different pages. The session variables last until the browser window is open. But nowadays we can keep running the browsers in the background so they last forever until you kill the program.

The session variables last until the browser window is open

How to start a PHP session and store information?

To start a PHP session we use session_start() function and we use global variable: $_SESSION to set the sessions variables.

Lets create one PHP file session_1.php

session_1.php file :


<!DOCTYPE html>

$_SESSION["user"] = "The Coding Yogi";
$_SESSION["file"] = "session_1.php";

echo "You just created session variables.";

The above code shows how to start a session and store information in session variables. I've already used static information but you can ask for user information and store temporarily in sessions as well.

REMEMBER: Do not forget to add the session_start() at the first thing into your PHP file. It must be placed before you start writing any html code.

Checkout the output in the DEMO link given at the end of the article.

How to get the information stored in the session variables?

Let's create another file session_2.php to get the information we just stored in session_1.php


echo "Your user name is " . $_SESSION["user"] . ".
echo "You have set your session variables in " . $_SESSION["file"] . " file.";

The code above will give us the values stored in session variables 'user' and 'file'.

Always remember to start session in each PHP file if you want to use session variables in that file.

Checkout the output in the DEMO link given at the end of the article.

Now, that code we use if we know the names of the variables. But…

What if we don't know the names but want all the variables info stored in session?

We use print_r($_SESSION) to print out all those variables and their values. It will print an array.

For example:



We might wonder that how does this print_r() function actually work.

Let me explain that.

When we start a session, it uses an user-key (you know those weird looking numbers..lengthy.. which doesn't make any sense? Something like that…) to set all the variables on the user's device. Whenever you open the session, it will search for the same key and if it finds it, it will show the variables in an array. If it doesn't then it will just display an empty array.

How to change those values of session variables? How to modify session or a single variable?

To modify a session variable, simply override the values by writing down the same code we wrote while creating session variables. 


<!DOCTYPE html>

$_SESSION["user"] = "Yogi";
$_SESSION["file"] = "session_1.php";

echo "You just updated session variables.";

How to destroy a session or just delete or remove session variables?

We use session_unset() to remove all the session variables.

If you want to remove any particular variable then just write down it's name into the bracket.

We use session_destroy() to destroy the session completely. 

Let's look at the example:


echo "You've removed all session variables and deleted the session as well."

The example above will remove the session variables and destroy the session completely. 

Try to print the session variables and see what happens. 😉 Let me know in the comments. 

Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
What is the difference between Loosely Typed Language and Strongly Typed Language?MiscellaneousHow to add Local State to a Class in React?ReactHow to add recaptcha version 3 to PHP website?PHP4 advanced ways to search ColleagueColleagueHow to use $IF operator in Envision Basic?Envision BasicWhat are Controlled Components in React?React