Contents
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
1
2
3
4
5
6
7
|
<?php
//Starting a PHP session
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-Â Â Â Â Â PHP creates a random unique identifier string of 32 hexadecimal numbers for that particular session. e.g. 9d5gpk75d2jj973hjkop2fc934s2578 (php session id)
- 2-Â Â Â Â Â To store that unique identifier a cookies is automatically sent to userâs computer. This cookie is called PHPSESSID.
- 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php
 // starts php session
 session_start();
 // setting variable values during session
 $_SESSION[‘UserID’]=‘1234’;
 $_SESSION[‘name’]=‘Mike’;
 $_SESSION[‘location’]=‘United States’;
print “session variables are assigned value.”;
?>
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
session_start();
//Simply display variable values created in test_session.php
print “My ID is: “ .$_SESSION[‘UserID’].“<br>”;
print “My Name is: “ .$_SESSION[‘name’]. “<br>”;
print “My Location is: “ .$_SESSION[‘location’];
?>
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
session_start();Â
if(isset($_SESSION[âUserIDâ]))
// isset is explained here
   unset($_SESSION[‘UserID’]);
   unset($_SESSION[‘name’]);
?>
|
In order to completely destroy a session use following:
1
2
3
4
5
6
7
|
<?php
session_start();
session_destroy();
?>
|
Leave A Comment?