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

In PHP session helps to store user information at client-side, 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 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.

Read Also: Session Vs Cookies in PHP

How to Start the PHP Session?

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

What is $_SESSION?

$_SESSION is a global constant in PHP. We can set and access the session data through the $_SESSION variable. In PHP $_SESSION is strictly case-sensitive we can not use $_session or $_Session.

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: Get 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”.

Here we will use the global variable $_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 session_start() function.

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 by just overwriting its original values.

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

In PHP, 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

Conclusion

I hope now you have a complete understanding of ‘how to set session in PHP’. Even more, PHP sessions are quite easy to implement.

Posted in PHP

Leave a Reply

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

Read previous post:
javascript array methods
JavaScript Array Methods Explained With Examples

The Array is used to store multiple values in a single variable. In JavaScript there are three types of Array:...

Close