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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
What happened to your Google Analytics Tracking ID, can’t find it?MiscellaneousHow to add menu to your WordPress theme?WordPressHow to convert a number rounding to a specified number of decimals in JavaScript?JavaScriptForcing the domain to serve securely using HTTPSMiscellaneousThe difference between Class declarations and Class expressions in JavaScriptJavaScriptKilling A Project Part 3: How can an organization ensure that a doomed project is killed as early as possible?Miscellaneous