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

查看:90
本文介绍了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)行无法正常工作. 我正在使用Windows XP + 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.

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

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打开/锁定文件,读取文件内容,反序列化数据,并可能会更新会话文件的上次使用"时间戳(在Unix上为atime).
  3. 如果星星和月亮与第五宫中的海王星上升点正确对齐,则会话垃圾收集器 MAY 启动并删除旧的会话文件.
  4. 垃圾收集器将愉快地遍历会话目录,并删除所有早于max_liftime的文件,但不会删除任何当前可打开/使用的文件.由于您尚未关闭会话,因此会话的文件仍在使用中,因此不会被删除.
  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(),否则旧的$ _SESSION数据将从以前的start()呼叫将仍然存在.会话文件可能已被删除,但是垃圾收集器在运行时不会触摸脚本内存中的内容.

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天全站免登陆