Session in PHP | How to start sessions, variables and destroy a session

What is session in PHP?

PHP Session is a way to store information in variables that can be used in multiple pages or across the website during that visit of a user. The php session stored information on server side unlike cookies, that stores information at user’s computer. Alternatively you can say PHP session variables scope is global unlike normal variable which scope is specific to that page or a function.

As your website becomes more complicated, there will be time when you need to store information to remember a particular visitor actions or data while navigating to other pages. A basic HTML site does not allow to store information that can be used across website. Sessions in PHP provides that mechanism.

For instance, in an ecommerce site storing shopping cart items, that has been added to basket during that visit. Users keep on surfing other pages of website, however selected items are still shown.

Similarly, storing login in information for a session.

The information stored in session variables is temporary and finishes as sessions ends or dies. For example user has closed the website.

How to Starting a PHP session

Now lets go through how session in php works. What we need to store user’s information and the using it to perform required actions.

PHP session start


So this what it takes to start a session in php. You simply write the command session_start(); . This command should be placed on top of the page even before html code. You must have to start the session in order to work with it.

So what actually happens when a session is started?

  1. 1-      PHP creates a random unique identifier string of 32 hexadecimal numbers for that particular session. e.g. 9d5gpk75d2jj973hjkop2fc934s2578 (php session id)
  2. 2-      To store that unique identifier a cookies is automatically sent to user’s computer. This cookie is called PHPSESSID.
  3. 3-      In the specified temporary directory on the server side a files is automatically created with prefix sess_[unique identifier code].

Working with PHP session variables

Lets go through now, by an example how values are stored in PHP session variables.

Name this php file as test_session.php


So after starting the session, this is how session variables are assigned values.

Like in above example use  $_SESSION[‘variable_name’]. $_SESSION[] is an associative array where it stores all session variables.

Using PHP Session Variable

Now create second file and write following code to print/echo session variable values. Name this file as print_ test_session.php


Output

My ID is: 1234

My Name is: Mike

My Location is: United States

So in above example we created session variable in one file, assigned values. And in other php file we simply print those session variables that carries the values.

PHP Session timeout

By default a session timeout period is set in php.ini file. A session will automatically be destroyed if a user’s browser is idle for specified period. You can change this time in php.ini or even specify session destroy time in your code file where you start session.

PHP.ini file

Go to php.ini file and locate these variable to see and change if required

//Sets to 60 mins

ini_set(‘session.gc_maxlifetime’,60*60);

ini_set(‘session.gc_probability’,1);

ini_set(‘session.gc_divisor’,1);

PHP Session Destroy

Though PHP automatically destroys a session after timeout or user has left the website. You may need to destroy certain variables, which purpose has been accomplished or a session completely in explicit way.

Syntax of destroying session variables


In order to completely destroy a session use following:


Was this article helpful?

Related Articles

Leave A Comment?