Fri, 17 May 2024


PHP Sessions - The Basics

By: Nathalie, NetArt Media
Wed, 1 August 2018
PHP Sessions - The Basics

PHP sessions are incredible, to say the very least. For those who may not be aware, there are shopping cart systems on the internet which are based almost solely on sessions. I personally like to use both sessions and then eventually MySQL database tables when scripting up a shopping cart, but sessions still play a large part within the system.

Today I will cover basic session handling and present a few ways to display information passed from one page to another. Be sure to give this a try within your own development projects or websites.

Starting & Creating Sessions


Before we do anything with PHP sessions we have to create them. This is a very simple process and using a few variables and a Conditional Statement we can do what we want with them when it comes to displaying the results on a web page for visitors to view.


I'm setting 2 session variables here. Both are fairly common when it comes to creating a helpful user experience for visitors to a website. Quite often after logging into a secure area we find our username and/or a greeting with our username in it. And knowing the date is always helpful.

I'm on the West Coast of Canada, so I use the Los Angeles default time zone for displaying the date. The username session is usually passed after coming from a login page, but in this case I have made things simple and created a variable with my nickname in it.

After creating the session at the top of the page I then placed a link to another page to show you how sessions can be passed from one page to another. In a real-life scenario the user would probably be coming from a login form, which would pass the sessions over from there.

Upon clicking the link we end up on a page which displays the greeting message:

Hey there JT and welcome back to PHPS Tuts.
Today's date is April 24, 2016.

Within this page I checked to see that the sessions were set and if so, echo out the above message. If the sessions are not set an alternative message will be displayed. Here is the code I used on the second page.


In order to pass sessions from one page to another we must always start the page with start_session(); There is no exception in this regard.

Following that, we check to make sure the session variables are set and define the greetings we want to display whether they are or not. In a real life scenario you may want to redirect the user to another page if the sessions aren't set for some reason, or perhaps ask them if they have forgotten their login password. In this tutorial I welcomed them for the first time and invited them to browse the site.

This is a basic introduction to PHP sessions, with much more to come on them in the near future. We can perhaps put together a simple shopping cart script, showing how incredibly useful sessions can be - for the user, their experience and definitely for you as the website owner and/or administrator.


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox