php 还有30日到期,如何在30分钟后过期PHP会话?

文章目录

我需要保持一个会议30分钟,然后摧毁它。 你应该实现你自己的会话超时。他人提到的两个选项(_session.gc_maxlifetime < / em>和_会话。 cookielifetime)不可靠。我会解释原因。

首先

> session.gc_maxlifetime

> _session.gcmaxlifetime specifies the number of seconds after which data

will be seen as ‘garbage’ and cleaned up. Garbage collection occurs during

session start.

但垃圾收集器只能以_session.gcprobability除以_session.gcdivisor。而使用这些选项的默认值(分别为1和100),机会只有1%。

那么,您可以简单地调整这些值,以便更频繁地启动垃圾收集器。但是当垃圾收集器启动时,它会检查每个注册会话的有效性。这是成本密集型的​​。

此外,当使用PHP的默认_会话.savehandler文件中,会话数据存储在_session.savepath。使用该会话处理程序,会话数据的年龄根据文件的上次修改日期计算,而不是上次访问日期:

> Note: If you are using the default file-based session handler, your

filesystem must keep track of access times (atime). Windows FAT does not so

you will have to come up with another way to handle garbage collecting your

session if you are stuck with a FAT filesystem or any other filesystem where

atime tracking is not available. Since PHP 4.2.3 it has used mtime (modified

date) instead of atime. So, you won’t have problems with filesystems where

atime tracking is not available.

因此,会话数据文件会被删除,而会话本身仍然被认为是有效的,因为会话数据最近没有被更新。

第二:>session.cookie_lifetime> _session.cookielifetime specifies the lifetime of the cookie in seconds

which is sent to the browser. [ a¦]

是的,没错。这只会影响cookie的生命周期,会话本身可能仍然有效。但是服务器的任务是使会话无效,而不是客户端。所以这没有任何帮助。事实上,将session.cookie_lifetime设置为0会使session的Cookie成为一个真正的session cookie,它只在浏览器关闭之前有效。结论/最佳解决方案:最好的解决方案是实现你自己的会话超时。使用表示上次活动(即请求)时间的简单时间戳记,并使用每个请求进行更新:

if (isset($_SESSION[‘LAST_ACTIVITY’]) && (time() - $_SESSION[‘LAST_ACTIVITY’] > 1800)) {

// last request was more than 30 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

使用每个请求更新会话数据还会更改会话文件的修改日期,以便会话不会被垃圾收集器提前清除。

您还可以使用额外的时间戳记定期重新生成会话ID,以避免攻击会话,如会话修复:

if (!isset($_SESSION[‘CREATED’])) {

$_SESSION[‘CREATED’] = time();

} else if (time() - $_SESSION[‘CREATED’] > 1800) {

// session started more than 30 minutes ago

session_regenerate_id(true); // change session ID for the current session and invalidate old session ID

$_SESSION[‘CREATED’] = time(); // update creation time

}备注:

session.gc_maxlifetime应至少等于此自定义到期处理程序的生命周期(本例中为1800); 如果您希望在 活动30分钟之后而不是30分钟之后活动过期,那么您还需要使用` setcookie code> time()+ 60 30 `过期以保持会话cookie有效。

## Simple way of PHP session expiry in 30 minutes.

注意:如果你想改变时间,只需要改变你想要的时间,不要改变 60:这会给出分钟。

分钟:(30 60)

以天为单位:(n 24 60 60)n =没有天 -

## Login.php

session_start();

?>

Username

Password

if ($_POST[‘submit1’]) {

$v1 = “FirstUser”;

$v2 = “MyPassword”;

$v3 = $_POST[‘text’];

$v4 = $_POST[‘pwd’];

if ($v1 == $v3 && $v2 == $v4) {

$_SESSION[‘luser’] = $v1;

$_SESSION[‘start’] = time(); // Taking now logged in time.

// Ending a session in 30 minutes from the starting time.

$_SESSION[‘expire’] = $_SESSION[‘start’] + (30 * 60);

header(‘Location: http://localhost/somefolder/homepage.php‘);

} else {

echo “Please enter the username or password again!”;

}

}

?>

## HomePage.php

session_start();

if (!isset($_SESSION[‘luser’])) {

echo “Please Login again”;

echo “Click Here to Login“;

}

else {

$now = time(); // Checking the time now when home page starts.

if ($now > $_SESSION[‘expire’]) {

session_destroy();

echo “Your session has expired! Login here“;

}

else { //Starting this else one [else1]

?>

Welcome

echo $_SESSION['luser'];

echo "Log out";

?>

}

}

?>

LogOut.php<?php

session_start();

session_destroy();

header('Location: http://localhost/somefolder/login.php');

?>

未经作者同意,本文严禁转载,违者必究!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值