0

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
halfer
  • 19,824
  • 17
  • 99
  • 186
JOB
  • 85
  • 1
  • 1
  • 10

4 Answers4

6

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);
});
void
  • 36,090
  • 8
  • 62
  • 107
  • i'd suggest wrapping the JS in a timer/loop for this OP's requirements – Angry 84 Feb 11 '15 at 07:58
  • This answer sums it up simply JOB, the only thing you may do later on is start to use JSON so you can send and receive parameters back n forth using json_encode() – Angry 84 Feb 11 '15 at 08:03
  • Thanks alot @void, that is owsome, am sorted out, only that on your code in check.php you missed to use the "{ }" brackets for if and else but av debugged that here it is : $inactive) { echo "0"; } else {echo "1";} } $_SESSION['timeout'] = time(); ?> – JOB Feb 11 '15 at 08:46
1

You can't do that with php. You need to count time in javascript and make ajax request after that. Simplest way is probably use jQuery plugin like IdleTimeout.

mparafiniuk
  • 320
  • 1
  • 7
0

PHP code needs to be called/run to execute... It wont run in the background unless something is running it..

You have a few options:

1: Ajax as suggested by Shikhar.. You have to have something like a setTimeout in javascript which would call this every minute or how ever often

2: A cron job... but a cron job that checks every active session file and if it has expired, then you simply delete it (Kills a session)

3: Have some other means of loading a PHP file as often as you want to check

Even with the above, there is no sure way of doing this..

Cron job / PHP loading.. they need a trigger... and a cron job wont redirect your user the second their session is expired.

Your only choice here is to use ajax on a timer to post to the sever and check if the session is still valid, if not then redirect once the response comes back from PHP to the ajax call.

But even then, once a user has logged in and if they disable JS... you wont get no redirect or checking.

Angry 84
  • 2,935
  • 1
  • 25
  • 24
0

@JOB You need to fire an ajax request something like following

    <script type="text/javascript">
         jQuery(document).ready(function(){
             setInterval(function(){
                 jQuery.ajax({
                     url: 'http://localhost/your_script.php',
                     type: 'POST',
                     success:function(response){
                         alert("You are logged out");
                         return false;
                     }
                 });
             }, 1000);
         });
    </script>

This script will check the status of logout after every 1 second and logs out the user if the user is idle for the time decided by PHP script.

dev_khan
  • 709
  • 7
  • 16