How to Set Up Expiration to Sessions using PHP

In this tutorial I'm going to show you how to set up expiration on PHP sessions. I have created a simple login in this tutorial but if you prefer, may also learn Login with Validation in my previous tutorial. This tutorial will not give you a good design but will give you an idea in setting up session and expiry in PHP.

Creating our Login Form and Login Script

First, we create our login form with the login script. We have set up the session and expiry on the login script upon user submission if username and password is correct. We name this as "index.php".
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>How to Set Up Expiration to Sessions using PHP</title>
  5. </head>
  6. <body>
  7.         My username: neovic <br>
  8.         My password: devierte
  9.         <h2>Login Form</h2>
  10.         <form method="POST">
  11.                 Username: <input type="text" name="username" required>
  12.                 Password: <input type="password" name="password" required>
  13.                 <input type="submit" value="Login" name="login">
  14.         </form>
  15.         <br>
  16.         <?php
  17.                 if (isset($_POST['login'])){
  18.                         session_start();
  19.                        
  20.                         $username=$_POST['username'];
  21.                         $password=$_POST['password'];
  22.                        
  23.                         if ($username=="neovic" and $password=="devierte"){
  24.                                 $_SESSION['user']=$_POST['username'];
  25.                                 $_SESSION['expiry']=time() + (5*60); //set up session to expire within 5 min
  26.                                 header('location:goto.php');
  27.                         }
  28.                         else{
  29.                                 echo "Username or Password did not match!";
  30.                         }
  31.                        
  32.                 }
  33.         ?>
  34. </body>
  35. </html>

Creating our Goto Page

Next step is to create our goto page if the login input matches. We have included in this page a Logout link that will destroy our session. We name this as "goto.php".
  1. <?php
  2.     session_start();
  3.         $now = time();
  4.  
  5.     if (!isset($_SESSION['user'])) {
  6.         echo "Session not Set. Login"."<br>";
  7.                 ?>
  8.                         <a href="index.php">Login Here</a>
  9.                 <?php
  10.     }
  11.        
  12.     elseif ($now > $_SESSION['expiry']) {
  13.         session_destroy();
  14.         echo "Your session has expired! Login again"."<br>";
  15.                 ?>
  16.                         <a href="index.php">Login Here</a>
  17.                 <?php
  18.     }
  19.         else{
  20.                 ?>
  21.                 <!DOCTYPE html>
  22.                 <html>
  23.                 <head>
  24.                 <title>How to Set Up Expiration to Sessions using PHP</title>
  25.                 </head>
  26.                 <body>
  27.                 <h2>Welcome -  <?php echo $_SESSION['user']; ?></h2>
  28.                 <a href="logout.php">Logout</a>
  29.                 </body>
  30.                 </html>
  31.                 <?php
  32.         }
  33. ?>

Creating our Logout Script

Lastly, we create our logout script. This script will destroy our current session and will redirect us back to our login page. We name this script as "logout.php".
  1. <?php
  2.     session_start();
  3.     header('location: index.php');
  4. ?>

Add new comment