YogeshChauhan.com
The Complete Basic Explanation of PHP Sessions
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

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript Mar 3 Window setInterval() Method in JavaScript Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS

You might also like these

Steps to Secure a VPNMiscFive common features of Angular template syntax (with examples)AngularClean Form Input With These PHP Functions Before Inserting into DatabasePHPWordPress 5.7 is migrating to latest jQuery version and people are freaking outjQueryAn Example of Cross-site Scripting (XSS) Attack in PHP and How to Avoid It?PHPHow to check if the element exists using JavaScript and jQuery?JavaScript