Php/javascript Session Timeout With Warning
Solution 1:
I don't know how your website is done, but if done right, you should have a log in session and some sort of back end control system that denies any action if the previous action was made X minutes/hours ago and automatically expires the user. If you want to implement some client side code, you should have a javascript timer that alerts the user when expire time is about to be complete and you can also redirect the user to the homepage or log in page after the expire time is reached. This way all security features are on the back end and the javascript only works as a display measure for the display behavior.
UPDATE:
setInterval(function(){alert("Hey, your session is ending")},360000);
setInterval(function(){
redirect();
},720000);
functionredirect(){
document.location = "../logout.php"
}
UPDATE2:
setInterval(function(){
logout();
},600000);
functionlogout(){
if(confirm('Logout?'))
redirect();
elsealert('OK! keeping you logged in')
}
functionredirect(){
document.location = "../logout.php"
}
Every page with this code will ask after 10 minutes if the user wants to logout. This means your session cannot expire by itself, you must leave the control to the user
Solution 2:
Session Logout after 5 minutes
<scripttype="text/javascript">var interval;
$(document).on('mousemove', function () {
clearInterval(interval);
var coutdown = 5 * 60, $timer = $('.timer'); // After 5 minutes session expired (mouse button click code)
$timer.text(coutdown);
interval = setInterval(function () {
$timer.text(--coutdown);
if (coutdown === 0) {
alert("Session expired. User successfully logged out.");
window.location = "UserLogin.php";
}
}, 1000);
}).mousemove();
var interval;
$(document).on('keydown', function () {
clearInterval(interval);
var coutdown =5 * 60, $timer = $('.timer'); // After 5 minutes session expired (keyboard button press code)
$timer.text(coutdown);
interval = setInterval(function () {
$timer.text(--coutdown);
if (coutdown === 0) {
alert("Session expired User successfully logout.");
window.location = "UserLogin.php";
}
}, 1000);
}).mousemove();
<script>
<html><divclass="timer">
Time of session display on page
</div></html>
Post a Comment for "Php/javascript Session Timeout With Warning"