php - Killing session after certain time and update database -


i write following php code destroy session after 10 sec if user not on web page, not functioning properly. kills session after 10 sec not updating database. update database had refresh index.php page on browser updates database. want session destroyed automatically after 10 sec of start if close browser or inactive, , update database whether i'm on browser or not. don't want refreshing thing update database.

$inactive = 10;  // check see if $_session["timeout"] set if (isset($_session["timeout"])) {     // calculate session's "time live"     $sessionttl = time() - $_session["timeout"];     if ($sessionttl > $inactive) {          $new_status= 0;           $checkbox = "update online set status=($new_status) id=1 ";         $stmt = $conn->prepare($checkbox);         // execute query passing array toe execute();         $results = $stmt->execute(array($new_status));         // extract values $result                 $error = $stmt->errorinfo();         header("location:logout.php");        unset($_session['username1']);     } } 

this isn't possible way it. session bound single user , if user becomes inactive, doesn't open php script, cause action bound session.

if want detect inactivity, have store timestamp of last action in database , compare value current time.

update user set status=1, last_action=unix_timestamp() id=?; 

and set inactivity field, use this

update user set status=0 unix_timestamp() - last_action > 10; 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -