将函数关联到 session_start() 上的触发?

发布于 2024-09-06 12:59:32 字数 744 浏览 5 评论 0原文

我在网上搜索过,但未能找到解决以下挑战的方法:

我想以某种方式关联一个在独立于页面 调用 session_start 时执行的函数session_start 被调用。该函数旨在使用 get_define_constants() 恢复我存储在 $_SESSION 中的常量,以便任何人都可以再次使用它们PHP 页面。

这对我来说似乎很简单,但我很确定 PHP Session 扩展不支持用户定义事件的注册。我想知道是否有人能深入了解这个问题,以便我可以找出解决方案或停止尝试。

理想情况下,我想在运行时注册该函数,如下所示:

$constants = get_defined_constants();

$_SESSION["constants"] = $constants["user"];

function event_handler () {
    foreach ($_SESSION["constants"] as $key => $value) {
        define($key, $value);
    }
}

register_handler("session_start", "event_handler");

因此,在任何网页中,我都可以:

session_start();

并且我的所有常量将再次可用。

任何帮助将不胜感激。

I've searched the web but haven't been able to find a solution to the following challenge:

I'd like to somehow associate a function that executes when session_start is called independent of the page session_start is called in. The function is intended to restore constants I've stored in $_SESSION using get_defined_constants() so that they're available again to any PHP page.

This seems so straightforward to me but I'm pretty sure the PHP Session extension doesn't support the registration of user-defined events. I was wondering if anyone might have insight into this issue so I can either figure out the solution or stop trying.

Ideally, I'd like to just register the function at run-time like so:

$constants = get_defined_constants();

$_SESSION["constants"] = $constants["user"];

function event_handler () {
    foreach ($_SESSION["constants"] as $key => $value) {
        define($key, $value);
    }
}

register_handler("session_start", "event_handler");

So in any webpage, I could just go:

session_start();

and all my constants would be available again.

Any help would be greatly appreciated.

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

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

发布评论

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

评论(2

扛起拖把扫天下 2024-09-13 12:59:32

您正在寻找的是 session_set_save_handler()。尽管功能并不像您想要的那么精细,但它确实符合要求。

What you are looking for is session_set_save_handler(). Although the functionality isn't as granular as you desire, it does fit the bill.

简单气质女生网名 2024-09-13 12:59:32

除了使用自己的会话句柄之外,您还可以使用类来存储常量,将该对象存储在会话中并使用 魔术方法__wakeup,用于在session_start上重建常量对象时写入常量。

Besides of using your own session handle, you could also use a class to store the constants in, store that object in the session and use the magic method __wakeup to write the constants when the constants object is rebuild on session_start.

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