PHP session

PHP session

Last Updated on Mar 21, 2023

What are sessions?

Sessions are like cookies but instead of storing the key value pairs on the user’s computer, session stores them on the server.

You might ask if it’s not on my computer then how does it know it’s me? As you know HTTP requests do not maintain state. So when you go to example .com and later you go to example .com/page There is no way for the server to know they are the same users.

So with the help of sessions the very first time user goes to that page, the website stores something like a unique id on the user’s computer.

Then for the next request if it finds that unique Id it realizes that it’s the same user otherwise it creates a new id and stores it on user’s computer to make sure it would recognize them in the next request.

If you want to use the sessions the very first thing you should do is to start the session in the beginning of each page Then you can store values, read them, update them or delete them.

session_start();

Working with Sessions

Let’s see how it works. Here I have two pages:

index.php
example.php

Both of the files look like this:

<?php
session_start();

Create

In index.php I create the session. $_SESSION is another super global variable in php which you can use everywhere. Like cookies you store a set of key value pair with the help of $_SESSION variable

<?php
session_start();
$_SESSION['name'] = 'Amir';

Read

Now in my example.php file first I’ll check if the name exists and if it does I will echo it.

<?php
session_start();
if(isset($_SESSION['name'])){
    echo $_SESSION['name'];
}

In the example above if I go straight to example.php the very first time there will be no name because we set the value in index.php. So first go to index.php let the code put the name in the session and then go to example.php and you’ll see the magic

Update

By assigning a new value to the key of your session you can update it. Here in example.php I am going to change the name.

<?php
session_start();
$_SESSION['name'] = 'Amir Updated';

Delete

If you want to delete all the session you can unset them and then destroy them like this:

<?php
session_start();

session_unset();
session_destroy();

But if you want to only delete one of the key values you can unset it like this

unset is a function in php that destroys the variable.

<?php
session_start();
unset($_SESSION['name']);
https://youtu.be/1fjBWXsdZz8

Conclusion

Now you know about sessions in PHP.

I recommend you to open a PHP files and use create sessions, update them, read them and delete those sessions.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • sessions in PHP
  • create sessions
  • update sessions
  • sesssion superglobal variable
  • read sessions
  • delete sessions

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

Courses