PHP Session - Know all about PHP session

A visitor accessing our website is assigned a unique id called session id. A session is a way to store user information (in variables) to be used across the multiple pages (whole website).
  • In PHP a session is started with the function session_start()
  • Session variables are set with the PHP global variable $_SESSION
  • If you are working on sessions don't forget to mention session_start() function at the top of the document.
Example: php1.php
<?php
//starting session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
//set session variables
$_SESSION["favbird"]="peacock";
$_SESSION["favanimal"]="cat";
echo "session variables are set."
?>
</body>
</html>
Output::

How to get PHP session variable values?

We can access PHP session variable values by using super global variable $_SESSION["var_name"]

Example: php2.php
<?php
//starting session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
echo "Favourite bird is ".$_SESSION["favbird"]."<br>";
echo "Favourite animal is".$_SESSION["favanimal"];
?>
</body>
</html>
Output::
Another way to show all the session variable values for a user session is using the function print_r($_SESSION) 
<?php
//starting session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION);
?>
</body>
</html>
Output::

How to modify PHP variables?

You can modify PHP variables just as you modify normal variables.
Eg:
$_SESSION["favbird"]="peacock";
This can be modified as
$_SESSION["favbird"]="sparrow";

How to remove all global session variables?

To remove all global session variables we can use the function  session_unset(); , this function fill the session variables with null values.

How destroy a session?

To destory the session we use the function session_destroy();

The importance of PHP session is 

  1. It helps to maintain user state and user data all over the application.
  2. It helps us to know number of unique visitors visiting our website.
  3. It helps to recommend user the content he is engaged.
  4. E-commerce websites display a list of products user recently visited to. It is done in order to faciliate user in revisiting.
  5. Session would make user that the whole website is working for him. Example: Youtube will recommend same type of videos you are interested in.

What happens when you start a session in PHP?

  1. When we start a session in PHP, it creates a random 32 digit hexa-decimal value as an identifier for that particular session.
  2. It sends a cookie named PHPSESSID to the user's system (browser) . As the name says, the PHPSESSID stores session id of the session.
  3. A temporary file gets created on the server and it is stored in the specified directory. It names the file on the hexa-decimal id prefixed with sess_
    • Example:: Let session id be 4af5346acde45...     it will be stored as sess_4af5346acde45...
  4.  When user request to that particular website again, along with the request PHPSESSID cookie also sent. By PHPSESSID cookie the server gets the unique id string ( i.e session id ). Then the server look into it's directory for the file named with that string  inorder to get session variables' values. 
  5. When you close the browser or the website, it terminates (ends) the session after a certain period of time set by the programmer.

Where the session data is stored?

Session data is stored on the web server. Only session id is sent to the browser by the cookie named PHPSESSID, this session id sent back to the server using cookie when user requests another page in the website. So that the particular session could be identified at the server side.

Also know about


Post a Comment

0 Comments