Php 会话_destroy()的执行时间早于我需要的时间

Php 会话_destroy()的执行时间早于我需要的时间,php,session,Php,Session,所以我有这样的代码: $_SESSION['message'] = "Some Message"; $MSG = $_SESSION['message']; header('location: ../Pages/Error.php'); echo "<script type='text/javascript'>alert('$MSG');</script>"; //It has text here die(); //i set this only for testing

所以我有这样的代码:

$_SESSION['message'] = "Some Message";
$MSG = $_SESSION['message'];
header('location: ../Pages/Error.php');
echo "<script type='text/javascript'>alert('$MSG');</script>"; //It has text here
die(); //i set this only for testing purpose
session_destroy();
die();

有人能解释一下为什么会发生这种情况吗?

之所以会发生这种情况,是因为您的浏览器在该脚本(您上面显示的脚本)实际将会话数据写入存储器之前重定向到了新页面。会话数据保存在内存中,直到脚本完成或手动调用为止

这意味着要使示例正常工作,您必须在输出重定向头之前写入会话数据,如下所示:

$_SESSION['message'] = "Some Message";
$MSG = $_SESSION['message'];
session_write_close();
header('location: ../Pages/Error.php');
echo "<script type='text/javascript'>alert('$MSG');</script>"; //It has text here
die(); //i set this only for testing purpose
session_destroy();
die();
$\u会话['message']=“一些消息”;
$MSG=$_会话['message'];
会话写入关闭();
标题('location:../Pages/Error.php');
回显“警报('$MSG');”//这里有文本
模具()//我设置此项仅用于测试目的
会话_destroy();
模具();

这也意味着您的会话不是您最初怀疑的“被破坏的速度比重定向的速度快”。

发生这种情况是因为您的浏览器在该脚本(上面显示的脚本)实际将会话数据写入存储之前正在重定向到新页面。会话数据保存在内存中,直到脚本完成或手动调用为止

这意味着要使示例正常工作,您必须在输出重定向头之前写入会话数据,如下所示:

$_SESSION['message'] = "Some Message";
$MSG = $_SESSION['message'];
session_write_close();
header('location: ../Pages/Error.php');
echo "<script type='text/javascript'>alert('$MSG');</script>"; //It has text here
die(); //i set this only for testing purpose
session_destroy();
die();
$\u会话['message']=“一些消息”;
$MSG=$_会话['message'];
会话写入关闭();
标题('location:../Pages/Error.php');
回显“警报('$MSG');”//这里有文本
模具()//我设置此项仅用于测试目的
会话_destroy();
模具();
这也意味着您的会话不是像您最初怀疑的那样“被破坏的速度比重定向的速度快”。

这就是发生的情况(例如时间)。请注意,这并不是自动发生的

1 $_SESSION['message'] = "Some Message";
2 $MSG = $_SESSION['message'];
3 header('location: ../Pages/Error.php');
4 echo "<script type='text/javascript'>alert('$MSG');</script>"; //It has text here
5 You do not have buffering, so header and echo go out to the browser.
6 session_destroy();
8           The browser receives the Location header
7 The server destroys the session file.
9           The browser requests the page
10          The server receives the request
11          The server tries to load the session file. Duh! It's not there!
在这种情况下,在
session\u start()
之后立即添加“”,或者在可能需要读取会话但不更改会话的文件中尽快添加“”,似乎会神奇地加快应用程序的速度(删除任何不需要的会话\u start()也会发生同样的情况),但该框架可能会使这一目标变得复杂:

----sxxxxx---
--------SwwXXX---
-----SwwXXXXX----
-------SwXXXXX---
--------SwXXX----
||“感知”用户时间下降了40%
这就是发生的事情(时间只是一个例子)。请注意,这并不是自动发生的

1 $_SESSION['message'] = "Some Message";
2 $MSG = $_SESSION['message'];
3 header('location: ../Pages/Error.php');
4 echo "<script type='text/javascript'>alert('$MSG');</script>"; //It has text here
5 You do not have buffering, so header and echo go out to the browser.
6 session_destroy();
8           The browser receives the Location header
7 The server destroys the session file.
9           The browser requests the page
10          The server receives the request
11          The server tries to load the session file. Duh! It's not there!
在这种情况下,在
session\u start()
之后立即添加“”,或者在可能需要读取会话但不更改会话的文件中尽快添加“”,似乎会神奇地加快应用程序的速度(删除任何不需要的会话\u start()也会发生同样的情况),但该框架可能会使这一目标变得复杂:

----sxxxxx---
--------SwwXXX---
-----SwwXXXXX----
-------SwXXXXX---
--------SwXXX----
||“感知”用户时间下降了40%

我不明白。我编辑了我的问题并发布了它,但没有我为测试所做的行。“你能看一下吗?”亚历克斯阿里斯特同样的推理也适用。如果在将会话数据写入存储器之前重定向浏览器,则当处理此请求尚未完成时,新请求将已启动,因此此时未写入任何会话数据。但在我的代码的其他部分中,我没有使用
session_write_close()任何地方,它都能以某种方式工作。@Aleksaritic好吧,这几乎是一个错误。有些时候你可能运气好,有些时候你可能运气不好。但是,它与破坏你的会话imho无关。我不明白。我编辑了我的问题并发布了它,但没有我为测试所做的行。“你能看一下吗?”亚历克斯阿里斯特同样的推理也适用。如果在将会话数据写入存储器之前重定向浏览器,则当处理此请求尚未完成时,新请求将已启动,因此此时未写入任何会话数据。但在我的代码的其他部分中,我没有使用
session_write_close()任何地方,它都能以某种方式工作。@Aleksaritic好吧,这几乎是一个错误。有些时候你可能运气好,有些时候你可能运气不好。但是,它与销毁会话imho没有任何关系。
header('location:../Pages/Error.php')&die('alert('.$\u session['message'])
header('location:../Pages/Error.php')&die('alert('.$'u SESSION['message'].'))
-----SXXXXXXXX----------
--------SwwwwwwwwwXXX---
-----SwwwwwXXXXX--------
-------SwwwwwXXXXX------
--------SwwwwwwXXX------
     |<------------>|
-----SXXXXXXXX---
--------SwwXXX---
-----SwwXXXXX----
-------SwXXXXX---
--------SwXXX----
     |<----->| "perceived" user time goes down a good 40%