YogeshChauhan . com

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 :


session_start();

<!DOCTYPE html>
<html>
<body>

$_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


session_start();

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:


session_start();

print_r($_SESSION);

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. 


session_start();

<!DOCTYPE html>
<html>
<body>

$_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:


session_start();

session_unset();
session_destroy();
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. 

dreamhost

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 uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 5 Use inline if to make a shorter conditional syntax in React May 4 What is Conditional Rendering in React? May 3 How does @extend rule work in SCSS (Sass)? May 2 How different is Handling Events in React vs HTML DOM? May 2 How to create bouncing balls using HTML canvas and JavaScript? Apr 30 HTML canvas methods

You might also like these

The order Property in Flex Items in CSSCSS3 Common Usability Mistakes In Web DesignUI/UXSQL ALL OperatorSQL/MySQLHow to implement NSNumberFormatter in Swift for currency?SwiftWordPress: How to print ACF repeater field values?WordPressPostgreSQL BETWEENPostgres