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 isset and empty
Mar 21, 2023 programming

PHP isset and empty

Today we are going to learn about isset and empty in PHP. isset and empty are two functions that help us check the existence of a value. They are way more useful than you think. ...

5 Min Read Read More
PHP Array Search
Feb 14, 2023 programming

PHP Array Search

Today we are going to talk about array search function in PHP. array search is a very useful function that helps you find the key of a value you’re looking for. ...

3 Min Read Read More
PHP Array Intersect
Feb 14, 2023 programming

PHP Array Intersect

Today we are going to talk about array intersect functions in PHP .If you want to find the items in your array that are present in the other arrays, array intersect is your friend. ...

5 Min Read Read More
PHP Callback Functions
Jan 08, 2023 programming

PHP Callback Functions

Today we are going to talk about callback functions in PHP. sometimes we need to pass a function as an argument of another function, that's exactly what callback function is. ...

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