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

The PHP session helps store user information at the client-side, which can be accessed on multiple pages.

What is the Session in PHP?

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

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.

In PHP, we can access and modify sessions through the $_SESSION variable.

Read Also: Session Vs Cookies in PHP

What is the $_SESSION in PHP?

The $_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. Before setting a session, we have to start a session.

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.

Syntax:

session_start()

Example:

<?php
session_start();

// Other PHP Code
// Other PHP Code
// Other PHP Code
?>

Note: The PHP session’s default time out in the apache config is 30 minutes after the session starts. It can be modified via a php.ini file or HTACCESS file.

How to Use $_SESSION to Set a Session in PHP?

In the following steps, we will learn how to start a session and set a session variable in PHP with an example.

Step 1: Set session

Let’s create a page “create_session.php.” Here we generate session variables and put some data into 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 using the 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 ‘What is session in PHP with example.’ Even more, PHP sessions are quite easy to implement.

Happy Coding 🙂

Leave a Reply

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

Read previous post:
Required fields in PHP Form
PHP Form Required

PHP required fields means the input fields in a form must be filled if users wish to proceed with the...

Close