PHP Sessions

PHP Sessions are used to store information of individual users. It is an unique way to make data accessible across the various pages of an entire website. A session is a global variable stored on the server rather than user's computer. Each session is assigned a unique id or session id of an individual user, which is used to retrieve stored information on the server related to that user. At here the session id or unique id is used to avoid conflict between multiple users. With the sessions we can pass values from one page to another.

Whenever a user visits the website, a session ID is created for the user and the session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.


When a session is created, a cookie named PHPSESSID containing the unique session id is stored on the user’s computer and returned with every request to the server. Sessions have the capacity to store relatively large data compared to cookies. The session values are automatically deleted when the browser is closed, and if you want to store the values permanently, then you should store them in the database.

Start a PHP Session :

A session is started with the session_start() function. It starts a new session or if the session is already started then it resumes the existing one. The session_start() function will must be called at the beginning of the page.

Example :
<?php

  session_start();

?>

<html>
<body>

<?php

  $_SESSION["uname"] = "Alice";
  echo "Session information is set successfully. <br/>";

?>

</body>
</html>
Session variables are stored in associative array called $_SESSION[]. At the above example the value Alice is stored in the session variable uname. By the below statement, we can access the value
 $_SESSION["uname"];
Example :
<?php

  session_start();

?>

<html>
<body>

<?php

  echo "The session variable is : " . $_SESSION["uname"];

?>

</body>
</html>

Use of isset() function :

The isset() function is used to check if session variable is already created or not. for example :
<?php

  session_start();
  
  if(!isset( $_SESSION["uname"])) {
    $_SESSION["uname"] = "Alice";
  }

?>

<html>
<body>

<?php

  echo "The session variable is : " . $_SESSION["uname"];

?>

</body>
</html>

At the above example if the session variable is not created, then it will create the uname variable.

Modify a Session variable :

To change or modify a session variable, we just need to re-assign the new value. For example :
<?php

  session_start();

?>

<html>
<body>

<?php

  $_SESSION["uname"] = "Bob";
  echo "The session variable is : " . $_SESSION["uname"];

?>

</body>
</html>

Destroy a PHP Session :

To remove or unset all the session variables, we have to use the session_unset() function and to destroy the whole session, we have to use the session_destroy() function.

Example :
<?php

  session_start();

?>

<html>
<body>

<?php

  // remove or unset all the session 
  session_unset();

  // destroy the session
  session_destroy();

?>

</body>
</html>
And to unset only a single session variable we have to use unset() function.
<?php
  
  session_start();
  
  $_SESSION["uname"] = "Alice";

  echo $_SESSION["uname"] . "<br/>";

  unset($_SESSION["uname"]);

  if(!isset($_SESSION["uname"])) {
    echo "Session vaariable 'uname' is unset.";
  }

?>

Next Topic :