Free cookie consent management tool by TermsFeed Generator PHP session | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
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

Introduction to PHP and how to build your first web application in less than 10 minutes
Mar 21, 2023 programming

Introduction to PHP and how to build your first web application in less than 10 minutes

PHP is an open source scripting language for backend development. and we are going to build our very first PHP application together. ...

7 Min Read Read More
Top 5 websites to learn PHP for FREE
Dec 22, 2022 programming

Top 5 websites to learn PHP for FREE

Do you want to learn PHP but you don’t want to spend hundreds of dollars? You are not alone. There are many websites that help people like you and me to start our journey. ...

9 Min Read Read More
PHP break and continue
Mar 21, 2023 programming

PHP break and continue

Today we are going to learn about break and continue in PHP. These two words are going to help you a lot when dealing working with loops. you can avoid a lot of extra iterations and save time. ...

5 Min Read Read More
PHP execution time
Feb 15, 2023 programming

PHP execution time

Today we are going to talk about calculating execution time in PHP. Sometimes it’s a good idea to calculate the execution time of your functions and script as a whole. ...

3 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...