PHP Session

Session in Php

Using a PHP Session, you can store information in variables, to be used across multiple pages.By default, session variables last until the user closes the browser.

PHP session_start() function

A session is started using the session_start() function. Use the P.H.P global $_SESSION to set session variables.

Syntax :

bool session_start ( void )

Example :



P.H.P $_SESSION is an associative array that contains all session variables. It is used to set and get session variable values.

Example: Store information

$_SESSION["user"] = "Sachin";

Example: Get information

echo $_SESSION["user"];

Session Example







$_SESSION["user"] = "Sachin";

echo "Session information are set successfully.<br/>";


<a href=".....">Visit next page</a>



Session Counter Example



   if (!isset($_SESSION['counter'])) {

      $_SESSION['counter'] = 1;

   } else {



   echo ("Page Views: ".$_SESSION['counter']);


Destroying Session

P.H.P session_destroy() function is used to destroy all session variables completely.





Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *