PHP 会话超时 [英] PHP Session timeout

查看:30
本文介绍了PHP 会话超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户像这样登录时,我正在创建一个会话:

I am creating a session when a user logs in like so:

$_SESSION['id'] = $id;

如何在 X 分钟的会话中指定超时,然后在达到 X 分钟后让它执行功能或页面重定向??

How can I specify a timeout on that session of X minutes and then have it perform a function or a page redirect once it has reached X minutes??

我忘了提到由于不活动,我需要会话超时.

I forgot to mention that I need the session to timeout due to inactivity.

推荐答案

首先,存储用户上次提出请求的时间

first, store the last time the user made a request

<?php
  $_SESSION['timeout'] = time();
?>

在随后的请求中,检查他们在多久之前提出了上一个请求(在此示例中为 10 分钟)

in subsequent request, check how long ago they made their previous request (10 minutes in this example)

<?php
  if ($_SESSION['timeout'] + 10 * 60 < time()) {
     // session timed out
  } else {
     // session ok
  }
?>

这篇关于PHP 会话超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆