Session Timeout due to Inactivity in PHP
Login is the most important part of any project or software, but on the other hand, it need extra layer of security. If user forgets to logout the software an is away due to inactivity, in that case, Login Session needs to be Expired automatically in order to prevent it from any kind of misuse. In this tutorial, I am gonna explain you How to Expire Session in PHP after 15 minutes of inactivity by the user.
You may also like : File Size Validation using jQuery
EXPLAINATION
Functionality:
- When user or admin login into an account, PHP session is created .
- With the logout link provided in the design, user or admin can logout from the account
- If user forgets to logout or does not interact with the software for about 15 minutes, the account sign out automatically.
Logic Behind This Working?
Firstly, we have to create a login account interface with full working of inputting username and password
you can learn to create Login registration System Here
<?php
if (valid user) {
// in your login check set this two session variables
$_SESSION["username"] = "your user name";
$_SESSION['last_login_timestamp'] = time();
}
?>
In your login check page, You have to create two PHP session variables
This code needs to be included in every page except logout page.(caution : Dont put this code in logout file)
<?php
if (isset($_SESSION["username"])) {
// only if user is logged in perform this check
if ((time() - $_SESSION['last_login_timestamp']) > 900) {
header("location:logout.php");
exit;
} else {
$_SESSION['last_login_timestamp'] = time();
}
}
?>
Logout page will destroy all the session data or variables and take user back to login page (index page)
<?php
session_start();
unset($_SESSION);
session_destroy();
header("location:index.php");
?>
Password for Source code : 123456
Session Timeout due to inactivity | Techs Awesome
Reviewed by Techs Awesome
on
November 11, 2020
Rating:
Reviewed by Techs Awesome
on
November 11, 2020
Rating:


No comments: