从官方php手册:
session_register_shutdown- 会话关闭功能注册
session_write_close()为关闭功能.
那么,会话关闭意味着什么?session_write_close和这之间有什么区别?
为了说清楚,这个功能实际上做了什么?我们何时使用它?
似乎很少有人在网上使用和讨论这个功能.
这个功能实际上很少使用,互联网上几乎没有讨论,我终于从源代码评论中找到了答案:
/* This function is registered itself as a shutdown function by
* session_set_save_handler($obj). The reason we now register another
* shutdown function is in case the user registered their own shutdown
* function after calling session_set_save_handler(), which expects
* the session still to be available.
Run Code Online (Sandbox Code Playgroud)
来自:https://github.com/php/php-src/blob/master/ext/session/session.c
而且,显然,与官方手册相比,这清楚地说明了它的确切做法.
这个问题看起来很傻.
这是一个公平的问题。我也对文档感到困惑。
读了十几遍之后,我得出的结论是,该函数只是代码的快捷版本
register_shutdown_function('session_write_close');
如果您在 php 脚本中调用 die() 或 exit,会话将不会正确关闭(特别是如果您有自定义会话处理程序)。这个功能无非就是一个快捷方式。
我的特定会话处理程序的引导代码如下所示:
// Set the session handlers to the custom functions.
$handler = new SQLSrvSessionHandler();
session_set_save_handler(
array($handler, 'sessionOpen'),
array($handler, 'sessionClose'),
array($handler, 'sessionRead'),
array($handler, 'sessionWrite'),
array($handler, 'sessionDestroy'),
array($handler, 'sessionGC')
);
session_register_shutdown();
Run Code Online (Sandbox Code Playgroud)
该函数session_register_shutdown确保我的函数SQLSrvSessionHandler::sessionClose,SQLSrvSessionHandler::sessionWrite即使我运行 die 或 exit 语句也会被调用。
我也发现这个答案很有帮助。