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

PHP cookies
Mar 21, 2023 programming

PHP cookies

Today we are going to learn about cookies in PHP. Cookies are like files or better say content that the website/server would embed on your computer through your browser. ...

12 Min Read Read More
PHP Database
Mar 22, 2023 programming

PHP Database

Today we are going to talk about working with a database in PHP. working with databases is a very important part of web applications. you should be able to store data and read them. ...

14 Min Read Read More
PHP CSV Files
Mar 21, 2023 programming

PHP CSV Files

Today we are going to talk about working with a csv files in PHP. Comma Separated Values (csv) is a common way to store the data in a file and it's important to know how to work with them. ...

5 Min Read Read More
TypeScript Beginner to Advanced 2025: The Complete Guide
Jul 18, 2025 programming

TypeScript Beginner to Advanced 2025: The Complete Guide

TypeScript is no longer optional for serious JavaScript developers, it’s essential. Whether you're building modern front-end applications with React, scaling backend services with Node.js, or contributing to large open-source projects, TypeScript is the industry standard in 2025. ...

23 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 ...