PHP - ini_set('session.gc_maxlifetime', 5) - 为什么不结束会话? [英] PHP - ini_set('session.gc_maxlifetime', 5) - Why it doesn't end the session?

查看:23
本文介绍了PHP - ini_set('session.gc_maxlifetime', 5) - 为什么不结束会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP脚本如下:

<?php // continue.php
ini_set('session.gc_maxlifetime', 5);
session_start();
echo ini_get('session.gc_maxlifetime');
// wait for 7 seconds
usleep(7000000);
if (isset($_SESSION['username']))
{
    $username = $_SESSION['username'];
    $password = $_SESSION['password'];
    $forename = $_SESSION['forename'];
    $surname  = $_SESSION['surname'];

    echo "Welcome back $forename.<br />
          Your full name is $forename $surname.<br />
          Your username is '$username'
          and your password is '$password'.";
}
else echo "Please <a href=authenticate2.php>click here</a> to log in.";

?>

基于超时(即 5 秒),脚本不应打印任何内容.但是,我仍然收到以下消息

Based on the timeout (i.e. 5 seconds), the script should not print out anything. However, I still receive the following message

5Welcome back Bill. Your full name is Bill Smith. Your username is 'bsmith' and your password is 'mysecret'.

似乎 ini_set('session.gc_maxlifetime', 5) 行没有正常工作.我使用的是 windowsXP + XAMMP.

It seems that the line ini_set('session.gc_maxlifetime', 5) doesn't work as it should be. I am using windowsXP + XAMMP.

你能告诉我如何使它工作吗?

May you tell me how to make it work?

谢谢

推荐答案

即使垃圾收集器启动并删除了您使用 session_start() 打开/读取的会话文件,它也不会进入该特定 PHP 进程的内容并删除 $_SESSION 对象数组.

Even if the garbage collector kicked in and deleted the session file you opened/read with session_start(), it will NOT reach into the guts of that particular PHP process and delete the $_SESSION object array.

假设您使用标准的基于文件的会话处理程序(其中包含 serialize()$_SESSION 副本),这就是会发生的情况.

Assuming you're on the standard file-based session handler (which contains a serialize()'d copy of $_SESSION), here's what happens.

  1. 会话文件位于其临时目录中
  2. session_start(),导致PHP打开/锁定文件,读取其内容,反序列化数据,顺便说一句,可能会更新会话文件的上次使用"时间戳(atime on Unix box).
  3. 如果星星和月亮与第五宫的上升海王星正确对齐,会话垃圾收集器可能启动并删除旧的会话文件.
  4. 垃圾收集器将愉快地遍历会话目录,并删除任何早于 max_lifttime 的文件,但不会删除任何当前打开/使用中的文件.由于您尚未关闭() 会话,您的会话文件仍在使用中,因此不会被删除.
  1. The session file sits in its temp directory
  2. You session_start(), causing PHP to open/lock the file, read its contents, deserialize the data, and incidentally, possibly update the session file's "last used" timestamp (atime on Unix boxes).
  3. If the stars and moon are aligned correctly with Neptune ascendant in the fifth house, the session garbage collector MAY fire up and delete old session files.
  4. The garbage collector will happily iterate through the session directory, and delete any files which are older than the max_liftime, BUT WILL NOT DELETE ANY FILES CURRENTLY OPEN/IN USE. Since you've not closed()'d your session, your session's file is still in use, so will not get deleted.

现在,如果你做了这样的事情:

Now, if you did something like this:

ini_set(...); // set GC probability to max, short session lifetime, etc...

session_start(); // populate $_SESSION
session_write_close(); // dump $_SESSION out to file, close file, release lock.

sleep(7); // Sleep for 7 seconds;

session_start(); // re-populate $_SESSION;

现在您可能会得到一个新的空白 $_SESSION,如果垃圾收集器决定启动.但是,除非您执行第二个 session_start(),来自上一个 start() 调用的旧 $_SESSION 数据仍将存在.会话文件可能已被删除,但垃圾收集器在脚本运行时不会触及脚本内存中的内容.

Now you might just end up with a fresh blank $_SESSION, IF the garbage collector decides to kick in. However, unless you do that second session_start(), the old $_SESSION data from the previous start() call WILL STILL BE PRESENT. The session file may have been trashed, but the garbage collector will not touch what's present in your script's memory as it runs.

这篇关于PHP - ini_set('session.gc_maxlifetime', 5) - 为什么不结束会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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