简体   繁体   中英

Automatic Logout in PHP upon inactivity only

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
    }
?>

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

没有刷新是不可能的,至少不是PHP。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM