在session_write_close之后读取会话变量

时间:2022-05-06 22:45:36

First, my concrete question. Should this work?

首先,我的具体问题。这有用吗?

<?php 
    session_start();
    $_SESSION['test']='TEST_CONTENT';

    echo '1: ',$_SESSION['test'];
    echo '<br>';

    session_write_close();

    echo '2: ',$_SESSION['test']
?>

Now, some background info. We have a web application with a Frameset (don't get me started... no, there's no money to change this) and we found that in some cases our SESSION variables were not being written to the database! After some arduous testing we found that two frames were being loaded concurrently, and while it almost never happens, the first-called frame finished after the second. The first-called frame was overwriting the session with a prior-made copy (since, at the end of script it writes the session).

现在,一些背景信息。我们有一个带有Frameset的Web应用程序(不要让我开始......不,没有钱可以改变它)我们发现在某些情况下我们的SESSION变量没有被写入数据库!经过一些艰苦的测试后,我们发现两个帧同时被加载,虽然它几乎从未发生过,但第一个被叫帧在第二帧之后完成。第一个被称为框架的是用先前制作的副本覆盖会话(因为,在脚本结束时它会写入会话)。

Our solution now is to try to call session_write_close() on the first-called frame as soon as we can, but we are concerned about being able to still read the session variables (with 100% certainty).

我们现在的解决方案是尽可能地尝试在第一个被调用的帧上调用session_write_close(),但是我们担心能够仍然读取会话变量(100%确定)。

1 个解决方案

#1


1  

Well thinking about it you could implement in a other way.

考虑一下你可以用另一种方式实现它。

How to implement it is quiet simple

如何实现它很安静简单

session_start();
$_SESSION['timestamp'] = time();

This will be put above in your script.

这将放在您的脚本中。

When the script stops it will be saved to your database, but wait stop, we are not done :P

当脚本停止时,它将保存到您的数据库中,但等待停止,我们没有完成:P

Then you check if the timestamp in the current record is heigher or lower. if it is lower then update, else forget to update and just do nothing. Should consider microtime(). because people love to press f5.

然后检查当前记录中的时间戳是更高还是更低。如果它更低然后更新,否则忘记更新,什么都不做。应该考虑microtime()。因为人们喜欢按f5。

So new idea, and I think it works a little bit better and saver for production to

这么新的想法,我觉得它的生产效果更好一些


To answer the comment:

回答评论:

Look at session handler, you can make your own function how php handles session. So you don't have to refactor you code, only add.

看看会话处理程序,你可以自己创建php处理会话的功能。因此,您不必重构代码,只需添加。

Then in the write part, you say to the database update the record with id x and where the timestamp is higher then the given value.

然后在写入部分中,您说数据库更新id为x的记录,并且时间戳高于给定值。

For the implementation you should add a column to the record timestamp/mincrotime. And in the session handler you save the time it was request. So you can make the comparison. Then you don't need to save the time in the session either :) (not in the $_SESSION, it is saved in the db!)

对于实现,您应该在记录时间戳/ mincrotime中添加一列。在会话处理程序中,您可以节省请求的时间。所以你可以进行比较。然后你不需要在会话中节省时间:)(不在$ _SESSION中,它保存在数据库中!)

#1


1  

Well thinking about it you could implement in a other way.

考虑一下你可以用另一种方式实现它。

How to implement it is quiet simple

如何实现它很安静简单

session_start();
$_SESSION['timestamp'] = time();

This will be put above in your script.

这将放在您的脚本中。

When the script stops it will be saved to your database, but wait stop, we are not done :P

当脚本停止时,它将保存到您的数据库中,但等待停止,我们没有完成:P

Then you check if the timestamp in the current record is heigher or lower. if it is lower then update, else forget to update and just do nothing. Should consider microtime(). because people love to press f5.

然后检查当前记录中的时间戳是更高还是更低。如果它更低然后更新,否则忘记更新,什么都不做。应该考虑microtime()。因为人们喜欢按f5。

So new idea, and I think it works a little bit better and saver for production to

这么新的想法,我觉得它的生产效果更好一些


To answer the comment:

回答评论:

Look at session handler, you can make your own function how php handles session. So you don't have to refactor you code, only add.

看看会话处理程序,你可以自己创建php处理会话的功能。因此,您不必重构代码,只需添加。

Then in the write part, you say to the database update the record with id x and where the timestamp is higher then the given value.

然后在写入部分中,您说数据库更新id为x的记录,并且时间戳高于给定值。

For the implementation you should add a column to the record timestamp/mincrotime. And in the session handler you save the time it was request. So you can make the comparison. Then you don't need to save the time in the session either :) (not in the $_SESSION, it is saved in the db!)

对于实现,您应该在记录时间戳/ mincrotime中添加一列。在会话处理程序中,您可以节省请求的时间。所以你可以进行比较。然后你不需要在会话中节省时间:)(不在$ _SESSION中,它保存在数据库中!)