php - 未调用 SessionHandlerInterface 写入方法

标签 php

所以我创建了一个实现 SessionhandlerInterface 的类并将其设置为 session 处理程序:

$sessionHandler = new SessionHandler();
session_set_save_handler($sessionHandler);
session_start();

问题是,写入函数从未被调用。

如果我使用 session_set_save_handler 的第二个参数并将其设置为 false:

session_set_save_handler($sessionHandler, false);

然后它就可以正常工作了。有人可以向我解释这种行为吗?我正在使用 PHP 5.4.6。

文档中写着:

When using objects as session save handlers, it is important to register the shutdown function with PHP to avoid unexpected side-effects from the way PHP internally destroys objects on shutdown and may prevent the write and close from being called. Typically you should register 'session_write_close' using the register_shutdown_function() function.

从 PHP 5.4.0 开始,您可以使用 session_register_shutdown() 或在使用 OOP 方法调用 session_set_save_handler() 并传递实现 SessionHandlerInterface 的实例时简单地使用“注册关闭”标志。

但我并不完全理解。

最佳答案

documentation陈述如下:

The "write" handler is not executed until after the output stream is closed. Thus, output from debugging statements in the "write" handler will never be seen in the browser. If debugging output is necessary, it is suggested that the debug output be written to a file instead.

这可能就是您使用简单的 echo 时看不到正在调用的函数的原因。

关于php - 未调用 SessionHandlerInterface 写入方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12583679/

相关文章:

php - 使用 Google map 帮助创建多个信息窗口

javascript - 从 PHP 插入 DOM 的动态变量。将该变量从 DOM 传递到 Javascript 时遇到问题

php - 需要建议 : Project Structure for Namespaced App Laravel

php - MySQL 全文 bool 模式搜索返回太多结果

php - jQuery Ajax 表单不在 iPhone 上提交

php - 下载时 Mysql blob 图像没有尺寸

javascript - 如何用 typeahead.js 显示图片?

php - 什么可能导致标题已经发送,我将如何追踪它?

javascript - window.open() 适用于 html 文件,但不适用于 php 文件

php - SQL查询按类别查找两个联合表的最大值