What Is the PHP Session, and How to Use $_SESSION

A session is used to store information which can be accessed in multiple pages.

When a user surfs on the internet, the webserver doesn’t know who is the user and what he does. So, session variables are used to identify the user by storing user’s information to be used on multiple pages.

Default time out for the session in apache config is 30 minutes. It can be modified via php.ini file or via HTACCESS file.

But, Session variables lose their data automatically after the user has closed the browser. Also, Session variables hold information about the current active user, which is typical for all pages.

How to start the PHP Session?

session_start() function is used to start session. This method should be written at the top in the PHP file before any include or require.

What is $_SESSION?

$_SESSION is a global variable which is stored at the server. We use $_SESSION to set and access session variables.

How to use $_SESSION?

Step 1: Set session

Let’s create a page “create_session.php”. Here we generate session variables and put some data in variables.

create_session.php

<?php
  session_start();   //start session
  $_SESSION["name"] = "NICK";                 // Set session variables
  $_SESSION["email"] = "[email protected]";
?>

Step 2: Getting Values of session Variables

Next, we create another page “getdata_session.php”.

Here we retrieve values of session variables which we set on our page “create_session.php”.

We use the global $_SESSION to retrieve data of all session variables. Another essential point is that we do not pass the values of session variables on each page. Instead of that, we retrieve session variables after starting the session by using function session_start().

getdata_session.php

<?php
  session_start();
?>
<!DOCTYPE html>
<html>
  <body>
  <?php
    //checks if session variables are set or not
    if(isset($_SESSION["name"]) && isset($_SESSION["email"])){
      // Echo session variables that were set on previous page
      echo "Name is " . $_SESSION["name"] ".<br>";
      echo "Email is " . $_SESSION["email"]  ".";
    }
  ?>
  </body>
</html>

Output:

Name is NICK
Email is [email protected]

Step 3: Modify session data

We can modify the session variables.

Let’s create the page “modify_session.php” to alter the session variables.

modify_session.php

<?php
  session_start();
?>
<!DOCTYPE html>
<html>
  <body>
  <?php
    // to change a session variable, just overwrite it
    $_SESSION["name"] = "TEST";
    echo $_SESSION["name"];
  ?>
  </body>
</html>

Output:

TEST

Step 4: Destroy or Unset Session

session_destroy() function destroys all sessions entirely, But session_unset() removes all session variables. However, both have a similar effect.

Example:

<?php
  // remove all session variables
  session_unset();
  // destroy the session
  session_destroy();
?>

Also Read: MySQLi Functions in PHP

Leave a Reply

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