3

I already had some research and i found a functional code to logout upon inactivity, but my problem is i still need to refresh my browser inorder to prompt my alert function to be able to logout it. I want a code function that will automatically log you out even if you won't do anything. BTW this is my code.

<?php
    if (time() - $_SESSION['timestamp'] > 30) {
?>
<script type="text/javascript">    
    alert("You Have Been inactive for 30 seconds");
     window.location.href = "logout.php"; //To my logout function
</script>
<?php
    } else {
      $_SESSION['timestamp'] = time(); //set new timestamp
    }
?>
xsami
  • 1,312
  • 16
  • 31
  • http://w3schools.invisionzone.com/index.php?showtopic=43874 – pala_ Apr 06 '15 at 23:52
  • thanks for than @pala_ but the thing there is it redirects u as soon as the time set is up. Not upon inactivity, but thanks though it helps me a bit – Marky Borja Apr 07 '15 at 00:15
  • ah, then this. http://stackoverflow.com/questions/667555/detecting-idle-time-in-javascript-elegantly – pala_ Apr 07 '15 at 00:16
  • Either way you'll need to refresh the page with this kind of thinking. What you want is an endpoint that you could hit to validate that the session is still valid (without applying an impression). Keep polling this endpoint and handle the the response on the client side. – slik Apr 07 '15 at 00:28

3 Answers3

1

pretty easy.

if you have a file that you include in each page you can implement it by adding the refresh script eg

header("Refresh: 1200; url = logoutsess.php");

this will redirect to a file logoutsess.php after 20mins therefore killing the session

dannjoroge
  • 608
  • 1
  • 8
  • 19
0

It's not possible without refreshing, at least not with PHP.

Gintoki
  • 142
  • 2
  • 16
0

Have you considered the use of AJAX for this instead of refreshing the browser every 30 seconds.

xhr requests verify_session.php and verify_session.php will check if the user has done anything within the last 30 seconds and print 1 or 0, your JavaScript will then redirect the browser to logout.php if it detects 0.

Michael Bush
  • 126
  • 2