使用回调从 php4 迁移会话到 php5 的奇怪行为

发布于 2024-07-26 02:40:16 字数 617 浏览 2 评论 0原文

我必须将使用 session_set_save_handler() 的 php4 应用程序迁移到 php5。

在 php4 中,一切都很好,但在 php5 中,回调函数无法再访问全局变量,这些变量是在调用 session_set_save_handler() 之前在页面上设置的。

在下面的示例中,全局变量 $g1 无法在 session_writer() 中访问(作为回调函数传递)

是否有对此行为的一些解释,或者您可以提供有关迁移会话的提示吗从 php4 到 5 的回调?

这是伪代码:

function session_writer($id,$vars) {
  global $g1;
  echo "g1 not defined here: ".is_object($g1);
}

global $g1;
$g1 = SomeObject(); //which is the DB connection for the session writer

session_set_save_handler($o,$c,$r,"session_writer",$d,$g);
session_start();

I have to migrate a php4 app that uses session_set_save_handler() to php5.

In php4 everything was fine, but in php5 the callback functions cannot access the global vars anymore, which were set on the page before session_set_save_handler() was called.

In the example below the global var $g1 cannot be accessed in the session_writer() (which is passed as a callback function)

Is there some explanation for this behavior or can you give a hint on migrating sessions with callbacks from php4 to 5?

This is the pseudo code:

function session_writer($id,$vars) {
  global $g1;
  echo "g1 not defined here: ".is_object($g1);
}

global $g1;
$g1 = SomeObject(); //which is the DB connection for the session writer

session_set_save_handler($o,$c,$r,"session_writer",$d,$g);
session_start();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不忘初心 2024-08-02 02:40:16

这实际上在文档中指出:

从 PHP 5.0.5 开始,写入和关闭处理程序在对象销毁后调用,因此不能使用对象或引发异常。 然而,对象析构函数可以使用会话。

可以从析构函数中调用session_write_close()来解决这个先有鸡还是先有蛋的问题。

本质上,您必须从 SomeObject 的析构函数中调用 session_write_close(),或者执行以下操作:

<?php register_shutdown_function("session_write_close"); ?>

这些解决方案中的任何一个都应该强制写入和关闭所有对象被销毁之前的会话,允许您保留原始的回调函数。

This is actually noted in the documentation:

As of PHP 5.0.5 the write and close handlers are called after object destruction and therefore cannot use objects or throw exceptions. The object destructors can however use sessions.

It is possible to call session_write_close() from the destructor to solve this chicken and egg problem.

Essentially, you'll have to call session_write_close() from the destructor of your SomeObject, or alternatively, do the following:

<?php register_shutdown_function("session_write_close"); ?>

Either of those solutions should force the writing and closing of the session before all objects are destroyed, allowing you to keep your original callback function.

爱*していゐ 2024-08-02 02:40:16

莫尔夫的回答指出了问题所在。 以下是更多信息:

session_write_close() 将标识符及其关联数据作为输入。 如果您想要进行最小的更改,您可能需要知道数据是 session_encode() 返回的数据(它是对 $_SESSION 数组的内容进行编码的字符串)。 该标识符是 session_id() 返回的内容。

Molf's answer identifies the problem. Here's some more info:

session_write_close() takes as input an identifier and the data associated with it. If you're going for minimal change, you'll probably need to know the data is what is returned by session_encode() (which is a string encoding the contents of the $_SESSION array). The identifier is what is returned by session_id().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文