简体   繁体   中英

PHP Online/Offline Script

I'm trying to code a script where a person logs in, and his last login time stamp is updated to the table. If he has logged in for more than 1 hour, immediately log him out even if he was interacting. I wonder how would I do that? I am using time() function to store the login time like this:

$lastlogin = time();

This has been marked duplicate: Automatic Logout after 15 minutes of inactive in php

But no, this is different. I want that to happen automatically without the user having to refresh the page. The answer on that question will only execute if the user interacts to the site, but I want the user to get logged out even if the user is away and not interacting.

I can't understand fully your want, but give you simple,

this just simple php for check you login, this example you user $_SESSION['USER'] for checking user login and set in this session array's and in this set is:

$_SESSION['user']['THE SESSION KEY FOR LOGIN (EX. USERNAME)'] = "User"
$_SESSION['user']['time'] = "900"

And this validation:

<?php
session_start();
//create session (JUST FOR ONE TIME)
if (isset($_SESSION['user']['THE SESSION KEY FOR LOGIN (EX. USERNAME)']) and time() -$_SESSION['user']['TIME'] > 900){
     unset($_SESSION['user']);
     echo "logout"
    }
?>

in client you must create Ajax to validation, i recommend if you dont fully understand, you just use ajax from jQuery in you page,

<script src"{YOUR JQUERY PLACE}">
setTimeout(function(){
$.ajax({
   url:"checker.php",
   dataType:"html",
   type:"GET",
   success:function(html){
      if(html == "logout"){
      //whatever you want..
      //window.location.reload(); for reload you page,
      }
   }
});
}, 3000); // this check from client for server every 3 second
</script>

and in you page you must validation again by PHP, this idea is, Server set time for user, in client PC, you script request to your server for checking this user. if user time's more than 15 mnt, this php send to server for refresh your browser and in you php for all page, you must give syntax logout if user time's more that 15 mnt.

Well, when the user logs in, you save the login time ( time() ) in a $_SESSION variable. Then, before any action is taken, you check for

if (time() >= $_SESSION["login_time"]+3600) {
    //Log the user out
}

3600 is the number of seconds in an hour, so you're basically checking "Is now later than time when the user logged in plus one hour ?". And if that is true, you log the user out.

The reason this is put in a session is because if the session itself expires, your user is logged out regardless.

Here is an example to reload your window every 3 seconds. This can be used in conjunction with AJAX to make a request and handle the response such that you can logout the user.

link

 var interval = null;

 $(function(){
      interval = setInterval(reloadWindow, 3000);
  });

 function reloadWindow(){
    location.reload()
 }

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