Open In App

How to Destroy Session After Some Time in PHP ?

Improve
Improve
Like Article
Like
Save
Share
Report

In PHP, we create sessions for the user who is logged in and make that user online till the user log out of that session. It can be done by clicking on the logout button or by destroying that session after a fixed time. 

By default the expiry time of any particular session that is created is 1440 secs i.e. (24*60) i.e. 24 minutes. But in some cases, we need to change the default time accordingly.

We can do that in 2 ways.

1. We can change it in the php.ini file, and change the configuration, but that will change the default time for all the sites working on that server and that will be a hindrance to all other sites.So the second option is preferable.

2. We can logically change the destroy time of the session. We take the time of the creation of the session by calculating the system current time and as the user browses to different pages of the script will check for the expiry time i.e. is explicitly declared as session-expiry.

File Structure:

index.php: If you enter wrong credentials it will throw an error, If you enter correct credentials you will be redirected to “HomePage.php” and destroy session after 1 minute.The developer can change the time accordingly.

PHP




<?php
   
    session_start();
   
    if(@$_POST['submit']) {
        $username = $_POST['email'];
        $password = $_POST['pwd'];
   
        if($username =="GFG@gmail.com" && $password=="gfg123") {
            $_SESSION['user'] = $username;
  
            // Taking current system Time
            $_SESSION['start'] = time(); 
  
            // Destroying session after 1 minute
            $_SESSION['expire'] = $_SESSION['start'] + (1 * 10) ; 
               
            header('Location: HomePage.php');
        }
        else {
            $err= "<font color='red'>Invalid user login </font>";
        }
    }
?>
  
<html>
  
<head>
    <style>
        h2 {
            text-align: center;
        }
    </style>
</head>
  
<body>
    <h2 style="color:green">GeeksforGeeks</h2>
    <form method="post">
  
        <table align="center">
            <tr>
                <td>
                    <?php echo @$err;?>
                </td>
            </tr>
            <tr>
                <td>Username </td>
                <td><input type="email" name="email" 
                    placeholder="GFG@gmail.com" required>
                </td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="pwd" 
                    placeholder="gfg123" required>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" 
                        value="Sign In" name="submit">
                </td>
            </tr>
        </table>
    </form>
</body>
  
</html>


HomePage.php




<?php
   
    session_start();
   
    if(!isset($_SESSION['user'])) {
        echo "<p align='center'>Want to login again";
        echo "<a href='index.php'>Click Here to Login</a></p>";
    }
    else {
        $now = time();
      
        if($now > $_SESSION['expire']) {
            session_destroy();
            echo "<p align='center'>Session has been destroyed!!";
            header("Location: index.php");  
        }
        else
?>
<html>
  
<head>
    <meta http-equiv="refresh" content="10">
</head>
  
<body>
    <p>
        Welcome
        <?php echo $_SESSION['user']; ?>
  
        <span style="float:right">
            <a href='logout.php'>LogOut</a></span>
  
        <p style="padding-top: 20px; 
            background:#CCCCCC;
            height: 400px; text-align: center">
            <span style="color:red; text-align:center">
                Your Session Will destroy after 1 minute
            </span>
            <br /><br />
        </p>
    </p>
    <?php
    }
}
  
?>
</body>
  
</html>


logout.php




<?php
  
session_start();
  
session_destroy();
  
header('location:index.php');
  
?>


Output:

Session destroy



Last Updated : 22 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads