Automatic Logout after 15 minutes of inactive in php

后端 未结 8 759
悲&欢浪女
悲&欢浪女 2020-12-04 15:51

I want to destroy session if users are not doing any kind of activity on website. At that time after 5 users automatically redirect on index page. How is it possible? Is pos

8条回答
  •  有刺的猬
    2020-12-04 16:30

    Pretty easy:

     if(time() - $_SESSION['timestamp'] > 900) { //subtract new timestamp from the old one
        echo"";
        unset($_SESSION['username'], $_SESSION['password'], $_SESSION['timestamp']);
        $_SESSION['logged_in'] = false;
        header("Location: " . index.php); //redirect to index.php
        exit;
    } else {
        $_SESSION['timestamp'] = time(); //set new timestamp
    }
    

提交回复
热议问题