如何在php中非活动x分钟后添加代码注销

时间:2016-07-03 12:22:13

标签: php

我有这段代码,但我不知道如何在php中非活动x分钟后添加注销

登录页面:

<html>
<head>
<title>login</title>
</head>
<body>
<form action="page1.php" method="get" >
ID:<input type="text" name="id"  />
Password:<input  type="password" name="pass" />
<input type="submit" value="login" />
</form>
</body>
</html>

获取页面:

<html>
<head>
<title> get</title>
<body>
<?php
session_start();
$get_id= $_GET['id'];
$get_pass = $_GET['pass'];
$_SESSION['user_id']=$get_id;
$_SESSION['user_pass']=$get_pass;
echo "welcome";
?>
</body>
</html>

现在我希望此代码在非活动10分钟后注销

2 个答案:

答案 0 :(得分:1)

您需要使用JavaScript来制作计数器,例如在x分钟后重定向到注销页面。

示例

<script>
  function logout() {
    window.location.replace("http://example.com/logout.php");
  }
  setTimeout(logout(), 10*60000);
</script>

答案 1 :(得分:0)

@Gumbo已经在这里回答了这个问题 How do I expire a PHP session after 30 minutes?

if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 600)) { // last request was more than 10 minutes ago session_unset(); // unset $_SESSION variable for the run-time session_destroy(); // destroy session data in storage } $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp