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. 

Checkout the DEMO

11 Comments

Jacky

Jul 15, 2020 09:07:19 pm

Hello, I check your new stuff like every week. Your writing style is awesome, keep doing what you're doing!

Barber

May 08, 2020 07:05:03 pm

I've been surfing online more than 2 hours today, yet I never found any interesting article like yours. It's pretty worth enough for me. In my view, if all web owners and bloggers made good content as you did, the net will be a lot more useful than ever before.

Gony

Mar 15, 2020 12:03:18 pm

You have made some decent points there. I looked on the web for additional information about the issue and found most people will go along with your views on this site.

Micky

Mar 15, 2020 03:03:19 am

I was recommended this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You're amazing! Thanks!

Vendy

Mar 15, 2020 01:03:45 am

What i do not realize is in truth how you're not actually much more neatly-favored than you might be right now. You are so intelligent. You recognize therefore significantly with regards to this matter, produced me individually imagine it from a lot of numerous angles. Its like men and women aren't interested unless it is something to accomplish with Woman gaga! Your personal stuffs outstanding. At all times handle it up!

Jimmy

Mar 14, 2020 10:03:41 am

I constantly spent my half an hour to read this web site's articles or reviews daily along with a mug of coffee.

Bianca

Mar 13, 2020 08:03:09 am

Amazing issues here. I'm very satisfied to look your post. Thank you a lot and I'm taking a look ahead to contact you. Will you kindly drop me a mail?

nice

Mar 12, 2020 02:03:34 pm

It is truly a nice and helpful piece of information. I'm glad that you just shared this helpful information with us. Please keep us informed like this. Thanks for sharing.

Jeannie

Mar 11, 2020 11:03:42 am

I am extremely impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you customize it yourself? Anyway keep up the excellent quality writing, it's rare to see a great blog like this one these days.

Molly

Mar 06, 2020 02:03:05 am

Very energetic blog, I enjoyed that bit. Will there be a part 2?

Harvey

Mar 05, 2020 04:03:22 pm

Howdy! I just wish to give you a big thumbs up for the excellent information you have right here on this post. I'll be coming back to your web site for more soon.

Leave a reply

Your email address will not be published. required fields are marked *