May 5, 2016
Create an Automatic Session Timeout/logout After 1 Minute of Inactivity Using PHP

Hi, friends in this tutorial I will show you how to create an automatic session timeout/logout after 1 minute of inactivity using PHP. Nowadays for the user login, the session concept is the most important. When the user forget to logout the page then the safety risk will increase and there is a chance for theft the data from the user. In this, I created a session variable and store current time into the variable. If user inactivity for the last 1 minute, then it will automatically log out from the system and redirect to the login page.
conn.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php //Database Connection $servername = "localhost"; $username = "root"; $password = ""; $db="test"; //Check Connection $conn = new mysqli($servername, $username, $password, $db); if(!$conn){ die ("Error on the Connection" . $conn->connect_error); } ?> |
login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php include 'conn.php'; // Database connection session_start(); if(isset($_POST["submit"])){ $_SESSION["user"] = $_POST["user"]; $_SESSION["pass"] = $_POST["pass"]; $_SESSION['last_time'] = time(); { if(!empty($_POST['user']) && !empty($_POST['pass'])){ $user = $_POST['user']; $pass = $_POST['pass']; //selecting database $query = mysqli_query($conn, "SELECT * FROM userpass WHERE user='".$user."' AND pass='".$pass."'"); $numrows= mysqli_num_rows($query); if($numrows !=0) { while($row = mysqli_fetch_assoc($query)) { $username=$row['user']; $password=$row['pass']; } if($user == $username && $pass == $password) { //Redirect Browser header('Location:welcome.php'); } } else { echo "Invalid Username or Password!"; } } else { echo "Required All fields!"; } } } ?> <!doctype html> <html> <body> <h1>User Login</h1> <form method="post"> <input type="text" name="user" id="user" placeholder="Enter Username"><br/><br/> <input type="password" name="pass" id="pass" placeholder="Enter Password"><br/><br/> <input type="submit" name="submit" value="Submit"> </form> </body> </html> |
welcome.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>Welcome to Time Session Login</title> </head> <body> <?php session_start(); if(isset($_SESSION["user"])) { if((time() - $_SESSION['last_time']) > 60) // Time in Seconds { header("location:logout.php"); } else { $_SESSION['last_time'] = time(); echo "<h1 align='center'>Welcome ".$_SESSION["user"]. "</h1>"; echo "<h3 align='center'>Automatic Logout after 1 minute of inactive</h3>"; echo "<p align='center'><a href='logout.php'>Logout</a></p>"; } } else { header('Location:login.php'); } ?> </body> </html> |
logout.php
1 2 3 4 5 |
<?php session_start(); session_destroy(); header('Location:login.php'); ?> |
Download
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]