Skip to content
Related Articles

Related Articles

How to register a variable in PHP session ?

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 03 Mar, 2022

The PHP session is required so that you can store the user information and use it on different pages of the browser. 

Approach: It creates a session with the name or any other useful information you want to store and access on different pages. Even after your page is closed you can access the information until the browser does not close. This is an important thing to understand if a browser is closed then the session is automatically destroyed. 

We can create the session by writing session_start() and destroy the session by using session_destroy(). You can access the session variable by writing $_session[“name”].

Let us understand how the session works from the following examples.

Example 1: In the following, you can create the session by entering the name. You can check the working of the session by opening a new page in the same browser and retrieving the session name. It gives you the name and this is how the session works and help us to store the information.

PHP




<?php
  //staring the session
  session_start();
?>
<html>
<head>   
    <meta charset="utf-8">
    <meta name="viewport" content=
          "width=device-width, initial-scale=1">
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
</head>
  
<body>
    <div class="container">
        <h2 style="color:green">GeeksforGeeks</h2>
        <strong>Session Manager</strong></br>
  
        <form method="POST">
            <br/>Enter Name: <input type="text" name="name"> <br />
            <br/>
            <div>
               <input type="submit" name="Submit1"
                       value="Create Session"
                       style="width:120;">
            </div><br/>
            <div>
              <input type="submit" name="Submit2" 
                     value="Retrieve Session"
                     style="width:120;">
            </div>
            <div><br/>
               <input type="submit" name="Submit3" 
                      value="Destroy Session"
                      style="width:120;">
            </div>
        </form>
    </div>
    <?php
  
    // Creating a session with name
    if(isset($_POST['Submit1']))
    
        $_SESSION["sname"]=$_POST["name"];
        echo "Session is created !!";
    }
      
    // Retrieve session by printing the session value
    if(isset($_POST['Submit2']))
    
        if(isset($_SESSION["sname"]))
        {
            echo "The Session Value = " . $_SESSION["sname"];
        }
        else
        {
            // If retrieve button is pressed and 
            // there is no session created
            // then this message will be printed
            echo "All Sessions Destroyed !!";
        }
    }
    if(isset($_POST['Submit3']))
    
        session_destroy();
    }
?>
  
</body>
</html>


 

Output:

Example 2: Let us take an example where we count how many times we visit the page. Take two PHP files naming “one.php” and “two.php” and set the link of both of them on the page using buttons. Now every time we click on a button then there is a counter which automatically increments the count because the session stores the value of clicks and changes accordingly. 

You can observe that every time we click another button the URL change and it increments that particular page count as shown below.

one.php




<?php
  
    session_start();
  
    if (!isset($_SESSION['pg1'])) {
        $_SESSION['pg1'] = 1;
    } else {
        $_SESSION['pg1'] += 1;
    }
  
?>
  
<!DOCTYPE html>
  
<body>
    <h2 style="color:green">GeeksforGeeks</h2>
    <strong>Session Manager</strong></br></br>
    <?php
    $i = 1;
    for ($i = 1; $i <= 2; $i++) {
        if (isset($_SESSION["pg$i"])) {
            echo "Page number $i count: " . $_SESSION["pg$i"] . '</br>';
        } else {
            echo "Page number $i count: " . 0 . '</br>';
        }
    }
    ?>
  
    <a href="one.php">Page number 1</a> </br>
    <a href="two.php">Page number 2</a> </br>
  
</body>
</html>


 

two.php




<?php
  
    session_start();
  
    if (!isset($_SESSION['pg2'])) {
        $_SESSION['pg2'] = 1;
    } else {
        $_SESSION['pg2'] += 1;
    }
?>
  
<!DOCTYPE html>
  
<body>
    <?php
    $i = 1;
    for ($i = 1; $i <= 2; $i++) {
        if (isset($_SESSION["pg$i"])) {
            echo "Page number $i count: " . $_SESSION["pg$i"] . '</br>';
        } else {
            echo "Page number $i count: " . 0 . '</br>';
        }
    }
    ?>
    <a href="one.php">Page number 1</a> </br>
    <a href="two.php">Page number 2</a> </br>
</body>
</html>


 

Output:

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!