将函数关联到 session_start() 上的触发?
我在网上搜索过,但未能找到解决以下挑战的方法:
我想以某种方式关联一个在独立于页面 调用
被调用。该函数旨在使用 session_start
时执行的函数session_startget_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找的是 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.
除了使用自己的会话句柄之外,您还可以使用类来存储常量,将该对象存储在会话中并使用 魔术方法
__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 onsession_start
.