Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic Logout in PHP upon inactivity only

Tags:

php

mysql

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
    }
?>
like image 234
Marky Borja Avatar asked Aug 05 '26 08:08

Marky Borja


1 Answers

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

like image 63
dannjoroge Avatar answered Aug 07 '26 23:08

dannjoroge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!