Session in PHP

Getting Started with PHP
Getting Started with PHP : Home
Introduction
Installation
Hello World
Data Types in PHP
Variables in PHP
Operators in PHP
Decision Making in PHP
Iterations in PHP
Functions in PHP
Arrays in PHP
Strings in PHP
Object Oriented PHP
Classes and Objects
Constructors
Inheritance
Object Interfaces
Static and Final
Advanced Features in PHP
GET and POST
Session in PHP
Cookies in PHP
Server Variables

Session
Session lets you store data of individual for accessing across multiple pages until the broswer is terminated i.e the data is stored for that period of access only. The session stores data for that access only and so very useful for tracking activity of user. Keep in mind that if we are accessing from two different browsers, both will have their own session data.

In PHP we need to manually start session before we can use session variables. This is done using session_start(). This function looks for the session id and if there is none it creates a new session id.
Session ID
Session ID is a unique string that is assigned to each session which lets PHP engine distinguish between different session and serve content/data of that session only. You can get session by using session_id() function.

$_SESSION is an array of all the session variables present. If you create an index in this array and assign any value to it, It will be automatically available in the session. You can use that at any page during that session and can be used in any scope i.e. it is superglobal.

<?php
// starting session
session_start();
echo session_id()."<br>";
// Creating new index in session array
$_SESSION['user'] = "user1";
function abc(){
    // calling session array elememt inside of a function
    echo $_SESSION['user'];
}
abc();

?>


Output
tn70bs3l16bsr21tf97e55ded2
user1

session_start() will look for a session Id and it won't get one because this is the first time we have called session_start() So it will create a new Session ID.
The session Id will be different in your case. You can get $_SESSION['user'] in any other page and you will get this same value, you can try that by creating two pages and assigning value to a session array element in one page and then accessing that in another page.
But remember, before using $_SESSION you must call session_start() at every page you are using it. Here we demonstrated that we can use $_SESSION in any scope by using it inside of a function.
Try making a second page similar to this to test it.

<?php
// starting session
session_start();
// accesing value of a variable defined on another page
echo $_SESSION['user'];

?>


Output
user1

session_start() will look for a Session ID and it will get one this time as we have already started session in previous page.
In this page we are trying to access a variable we didn't create on this page. This should give an error in case of a normal variable but Session Array lets you access data you defined on some other page as well. So this should give output we assigned it on the first page.
You can also get Session ID on this page using function session_id() and you will get the same session Id you got on the first page provided you haven't terminated broswer in between or destroyed the session.

You can destroy a session using function session_destroy().

Login to Track Your Progress