In this tutorial, I will show you how to automatically logout from system if user is inactive from last 15 minutes in php programming language. If your web application or web system have login system then session management is most important part. If user login to system and he is not use system and do his other work and forget to logout to system. Then at that time security risk will increase and other can stole data from system. So for that I have make login If user inactivity for last 15 minutes then after 15 minutes it will automatically logout from system and redirect to login page. For this I have create one session variable and store current time stamp into this variable. So User if login to system then current time stamp store into this session variable. On every page of user restriction I have update this session variable value. If user inactivity stop for last 15 minutes then it will automatically logout from system and redirect to login page.
Source Code
login.php
<?php
//login.php
session_start();
if(isset($_POST["sub"]))
{
$_SESSION["name"] = $_POST["name"];
$_SESSION['last_login_timestamp'] = time();
header("location:index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Webslesson Tutorial</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="jquery.js"></script>
<style>
#box
{
width:600px;
background:gray;
color:white;
margin:0 auto;
padding:10px;
text-align:center;
}
</style>
</head>
<body>
<div id="box">
<h2>Login</h2>
<form method="post">
<input type="text" name="name" id="name" placeholder="Enter Username" class="form-control" /><br />
<input type="password" name="pass" id="pass" placeholder="Enter Pass" class="form-control" /><br />
<input type="submit" name="sub" id="sub" class="btn btn-info" value="Submit" />
</form>
<br /><br />
</div>
</body>
</html>
index.php
<!DOCTYPE html>
<html>
<head>
<title>Webslesson Tutorial</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="jquery.js"></script>
<style>
#box
{
width:600px;
background:gray;
color:white;
margin:0 auto;
padding:10px;
text-align:center;
}
</style>
</head>
<body>
<?php
session_start();
if(isset($_SESSION["name"]))
{
if((time() - $_SESSION['last_login_timestamp']) > 60) // 900 = 15 * 60
{
header("location:logout.php");
}
else
{
$_SESSION['last_login_timestamp'] = time();
echo "<h1 align='center'>".$_SESSION["name"]."</h1>";
echo '<h1 align="center">'.$_SESSION['last_login_timestamp'].'</h1>';
echo "<p align='center'><a href='logout.php'>Logout</a></p>";
}
}
else
{
header('location:login.php');
}
?>
</body>
</html>
logout.php
<?php
//logout.php
session_start();
session_destroy();
header('location:login.php');
?>
thanks
ReplyDeleteVery helpful tutorial. Thanks.
ReplyDeleteSimple and useful one thx
ReplyDelete