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 Variables
Mar 21, 2023 programming

PHP Variables

Today we are going to learn about variables in PHP. Variables are very important and powerful in any programming language. So if you are learning PHP it's very important to know about variables. ...

5 Min Read Read More
PHP DataTypes
Mar 21, 2023 programming

PHP DataTypes

Today we are going to learn about Data Types in PHP. Data types are another important subject in programming. The better you know the data types you are working with, the more skillful you become. ...

5 Min Read Read More
PHP class
Mar 22, 2023 programming

PHP class

Today we are going to talk about classes in PHP. This is an introduction to a series of posts that would cover the topic of object oriented programming in PHP. class is the first one. ...

8 Min Read Read More
PHP Send SMTP Emails with PHPMailer
Mar 22, 2023 programming

PHP Send SMTP Emails with PHPMailer

Today we are going to send emails with the help of PHPMailer package. Sending emails is a very useful feature and in PHP it is very easy and powerful. ...

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