session in php

Sharing is caring!

In this tutorial you will learn how to store certain data on the server on a temporary basis using PHP session.

What is a Session

Although you can store data using cookies but it has some security issues. Since cookies are stored on user’s computer it is possible for an attacker to easily modify a cookie content to insert potentially harmful data in your application that might break your application.

Also every time the browser requests a URL to the server, all the cookie data for a Web site is automatically sent to the server within the request. It means if you have sotored 5 cookies on user’s system, each having 4KB in size, the browser needs to upload 20KB of data each time the user views a page, which can affect your site’s performance.

You can solve both of these issues by using the PHP session. A PHP session stores data on the server rather than user’s computer. In a session based environment, every user is identified through a unique number called session identifier or SID. This unique session ID is used to link each user with their own information on the server like emails, posts, etc.

Starting a PHP Session

Before you can store any information in session variables, you must first start up the session. To begin a new session, simply call the session_start() function. It will create a new session and generate a unique session ID for the user.

The PHP code in the example below simply starts a new session.

session_start(); // start up your PHP session! 

The session_start() function first checks for an existing session ID. If it finds one, i.e. if the session is already started, it sets up the session variables and if doesn’t, it starts a new session by creating a new session ID.

Storing a Session Variable

When you want to store user data in a session use the $_SESSION associative array. This is where you both store and retrieve session data. In previous versions of PHP there were other ways to perform this store operation, but it has been updated and this is the correct way to do it.

$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data


Pageviews = 1

In this example we learned how to store a variable to the session associative array $_SESSION and also how to retrieve data from that same array.

PHP Sessions: Using PHP’s isset Function

Now that you are able to store and retrieve data from the $_SESSION array, we can explore some of the real functionality of sessions. When you create a variable and store it in a session, you probably want to use it in the future. However, before you use a session variable it is necessary that you check to see if it exists already!

This is where PHP’s isset function comes in handy. isset is a function that takes any variable you want to use and checks to see if it has been set. That is, it has already been assigned a value.

With our previous example, we can create a very simple pageview counter by using isset to check if the pageview variable has already been created. If it has we can increment our counter. If it doesn’t exist we can create a pageview counter and set it to one. Here is the code to get this job done:

    $_SESSION['views'] = $_SESSION['views']+ 1;
    $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views']; 

The first time you run this script on a freshly opened browser the if statement will fail because no session variable views would have been stored yet. However, if you were to refresh the page the if statement would be true and the counter would increment by one. Each time you reran this script you would see an increase in view by one.

Cleaning and Destroying your Session

Although a session’s data is temporary and does not require that you explicitly clean after yourself, you may wish to delete some data for your various tasks.

Imagine that you were running an online business and a user used your website to buy your goods. The user has just completed a transaction on your website and you now want to remove everything from their shopping cart.


You can also completely destroy the session entirely by calling the session_destroy function.


Destroy will reset your session, so don’t call that function unless you are entirely comfortable losing all your stored session data!


Comments 21

  • A motivating discussion is worth comment. I do believe that you should publish more on this subject, it may not be a taboo matter but typically people don’t talk about such issues. To the next! Best wishes!!

  • Hello There. I found your blog using msn. This is a very well written article. I will be sure to bookmark it and come back to read more of your useful information. Thanks for the post. I’ll definitely return.

  • It’s in fact very difficult in this active life to listen news on Television, so I just use internet for that reason, and get the hottest information.

  • When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same comment. Is there any way you can remove people from that service? Many thanks!

  • When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same comment. Is there any way you can remove me from that service? Many thanks!

  • That is very interesting, You are an excessively skilled blogger. I have joined your feed and sit up for looking for extra of your magnificent post. Also, I’ve shared your website in my social networks!

  • Good day! I could have sworn I’ve been to this website before but after checking through some of the post I realized it’s new to me. Nonetheless, I’m definitely glad I found it and I’ll be bookmarking and checking back often!

  • Google

    The time to read or take a look at the content material or sites we’ve linked to below.

  • Howdy I am so happy I found your site, I really found you by mistake, while I was looking on Bing for something else, Nonetheless I am here now and would just like to say thanks for a remarkable post and a all round entertaining blog (I also love the theme/design), I don’t have time to read it all at the minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the excellent work.

  • Google

    Every once inside a even though we select blogs that we read. Listed beneath are the most current web sites that we pick out.

  • When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same comment. Is there any way you can remove me from that service? Bless you!

  • you have an important weblog here! would you like to make some invite posts on my blog?

  • When I originally commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get four emails with the identical comment. Is there any means you possibly can take away me from that service? Thanks!

  • excellent issues altogether, you just won a emblem new reader. What might you suggest in regards to your publish that you made some days ago? Any positive?

  • Awesome site you have here but I was curious about if you knew of any community forums that cover the same topics talked about here? I’d really love to be a part of community where I can get feed-back from other knowledgeable people that share the same interest. If you have any suggestions, please let me know. Kudos!|

  • Way cool! Some extremely valid points! I appreciate you penning this article and also the rest of the website is really good.

  • Hello, I read your blogs like every week. Your humoristic style is awesome, keep it up!

  • Hello, everything is going sound here and ofcourse every one is sharing facts, that’s in fact excellent, keep up writing.

  • Hello my loved one! I wish to say that this post is amazing, great written and include almost all vital infos. I’d like to see more posts like this .

  • I read this piece of writing fully concerning the resemblance of newest and earlier technologies, it’s awesome article.

Leave a Reply

Your email address will not be published. Required fields are marked *