How To Use Session And Session Variable With Example In PHP

admin_img Posted By Bajarangi soft , Posted On 22-09-2020

IN PHP Session support in PHP consists of a way to preserve certain data across subsequent accesses.Sessions are used to store data for individual users against a unique session ID.This can be used to persist state information between PHP page HTTP requests.Sessions store temporary data to use in multiple PHP pages to give a complete functional transaction for the same user and user accessing your web site is assigned with session id.

How to use session in PHP

Start a Session in PHP

  1. Create demo.php file.

  2. Create session in demo file by starting with the session_start() function.

  3. Session variables are set with the PHP global variable: $_SESSION.

  4. Implement code in demo.php

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Session in PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>Start session</h1>
    </div>
    <div class="well">
        <?php
        // Set session variables
        $_SESSION["Username"] = "Kiran";
        $_SESSION["UserId"] = "123456789";
        echo "Session variables are set.";
        ?>
    </div>
    <br>
</div>
</body>
</html>


Get PHP Session variable values from one page to another.

  1. Create demo2.php file.

  2. Create session in demo2 file by starting with the session_start() function.

  3. session variables are not created individually in new page, instead they are retrieved from the session we open at the demo.php file beginning session_start(). from that variable passed to demo2.php


  4.  
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Session in PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>

<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>Get PHP Session  variable values from one page to another</h1>
    </div>
    <br>
    <br>
    <br>
    <div class="well">
        <?php
        // Echo session variables that were set on previous page
        echo "<h1>User name is " . $_SESSION["Username"] . ".<br></h1>";
        echo "<h1>User Id is " . $_SESSION["UserId"] . ".</h1>";
        ?>
    </div>
    <br>
</div>
</body>
</html>
 

We can also Modify a PHP Session Variable 

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Session in PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>

<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>Start a Session in PHP</h1>
    </div>
    <br>
    <br>
    <h2></h2>
    <div class="well">
        <?php
        // Set session variables
        $_SESSION["Username"] = "Shiva";
        $_SESSION["UserId"] = "123456789";
        print_r($_SESSION);
        echo "Session variables are set.";
        ?>
    </div>
    <br>
</div>
</body>
</html>
 

Delete or Destroy created session id in a PHP Session

use session_unset() and session_destroy() u to remove all session variable

 

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
        <title>Session in PHP</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
        <div class="text-center">
                <h1>Delete or Destroy created session id in a PHP Session</h1>
            </div>
            <?php
            // remove all session variables
       session_unset();

        // destroy the session
        session_destroy();
        ?>
        <br>
</div>
</body>
</html>

 

Related Post