Session management in PHP

PHP session use to maintain information about your users or application. Session information is maintained user wise. It means a separate session maintain for each of your website's or web application's user. A session remain active till the time a user accesses a web application or a website, when user leave the website then session gets destroy automatically. You can also specify the session expiration time. Session variables are stored on the server side. Session variables and their values are available among multiple webpages of your website or application. So suppose you have set a session variable on a webpage, this variable can be access from another webpage of your website or web application. Session is also very useful for sharing information among the webpages of same website. What actually a session is? When user opens a website visit some webpages and close it. What he did is? At the time of opening the website he/she started a session with the website when the user visiting the different webpages of the website that time still he/she was in the same session. And when he/she leaves the website his/her session gets destroyed. So whenever users open a website or web application a session for each user with the website he/she is opening gets started. Through session you can maintain information about your website user. You can save information about how many users visited your website and at what time user likes to visit your site. For each user a unique session id gets generated so you can easily identify. Starting a SESSION: You can start a session with session_start() function. Without starting a session you cannot store values in session and even you cannot retrieve the value of existing session. So you will have to start a session with session_start() function on each webpage where you want to access session in your website. The example of session starting is mentioned below: Code


<?php 
  session_start(); 
?>
Storing value in Session Variable: You can store values in session variables with using the PHP $_SESSION variable. The example is mentioned below:

Code

<?php
  session_start();
  // store session data
  $_SESSION["FirstSessionVariable"]="Hello World!";
?>
Retrieving value from Session Variable

Code

<?php
  session_start();
  //retrieve session data
  echo "First Session Variable:".$_SESSION['FirstSessionVariable'];
?>
Output

First Session Variable = Hello World!

Destroying session: If you want to delete few session variables data then you can use the unset() function and if you want to destroy session completely then you can use session_destroy() function. The unset() function is used to unset or delete the specified session variable. Example of unset function is mentioned below:

Code

<?php
  unset($_SESSION["myFirstSessionVariable"]);
?>
If you want to delete session completely then you can completely destroy the session by calling the session_destroy() function. Example is mentioned below:

Code

<?php
  session_destroy();
?>