Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  51] [ 6]  / answers: 1 / hits: 16878  / 10 Years ago, wed, february 11, 2015, 12:00:00

I am writing a project in PHP, JavaScript and HTML. I have successfully done the automatic logout when the user is idle for 1 minute. But the problem comes in that I have to refresh the page for it to be executed and log me out.



Can somebody help me so that immediately 1 minute is over and the user is idle, the code will be executed and it will take me to the login page without me refreshing it?



Here is my code:



// check to see if $_SESSION['timeout'] is set
if(isset($_SESSION['timeout']) ) {
$session_life = time() - $_SESSION['timeout'];
if($session_life > $inactive)
{


echo<script type='text/javascript'>
window.alert('Your Session got Expired');
</script>;
header(Location: logout.php);
}
}

$_SESSION['timeout'] = time();
//Continuation of other codes

More From » php

 Answers
4

I guess the best way to implement is by using the combination of JS and PHP



check.php



if(isset($_SESSION['timeout']) ) {
$session_life = time() - $_SESSION['timeout'];
if($session_life > $inactive) echo 0;

else echo 1;
}

$_SESSION['timeout'] = time();


.js



$(document).ready(function(){
setTimeout(function(){
$.get(check.php, function(data){
if(data==0) window.location.href=logout.php;
});
},1*60*1000);
});


Or just wrap it in setInterval(function(){},1*60*1000) instead of setTimeout() if you want it to be checked after every one minute.



$(document).ready(function(){
setInterval(function(){
$.get(check.php, function(data){
if(data==0) window.location.href=logout.php;
});
},1*60*1000);
});

[#67874] Sunday, February 8, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jamaal

Total Points: 515
Total Questions: 102
Total Answers: 107

Location: France
Member since Thu, May 6, 2021
3 Years ago
;