ob_start() 可以影响包含的文件和必需的文件吗?
我有一些 php 文件“A”,它由 cron 从控制台调用(“php -q”命令)。该 php 文件需要 php 文件“B”。文件“B”在项目中的许多地方使用,并以 if(!isset($_SESSION)) session_start(); 开头,它在浏览器中工作正常,但是当由 cron 使用时,文件“A” ”需要文件“B”,文件“B”尝试启动会话,我收到“会话标头已发送”通知。我尝试在文件“A”中的
require_once("B")
之前包含 ob_start()
(当然还有 ob_clean()
>) 稍后,但错误仍然存在!
我做错了什么?我如何(从文件“A”)阻止文件“B”尝试将任何内容发送到控制台?
I have some php file "A" which is called by cron, from console ("php -q" command). That php file requires php file "B". File "B" is used at many places in project, and starts with if(!isset($_SESSION)) session_start();
It works fine from browser, but when used by cron, file "A" requires file "B", file "B" tries to start the session and i got "session headers sent" notice. I have tried to inculude ob_start()
in file "A", right before require_once("B")
(and of course, ob_clean()
) later, but error persists!
What am i doing wrong? How can i (from file "A") prevent file "B" from trying to send anything to console?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 CLI 中禁用 session.use_cookies php 脚本通过
ini_set()
或通过 php.ini 配置。这样session_start()
就不会尝试发送 cookie。您必须检查会话的缓存设置以及session_start()
发送有关缓存的 HTTP 标头。Disable session.use_cookies in your CLI php script via
ini_set()
or via php.ini configuration. This waysession_start()
don't try to send cookies. You have to check the cache settings for sessions as well assession_start()
send HTTP headers regarding caches, too.$_SESSION 主要基于 cookie,因此无法在 CLI 中工作
$_SESSION are mostly based on cookie so can't work in CLI
ob_start() 必须在任何输出之前调用。所以,也许在文件“A”的顶部。
另外,我认为 $_SESSION 是一个超全局变量并且总是被设置,因此 if(!isset($_SESSION)) 可能什么也不做(永远不会为真)。
ob_start() has to be called before any output. so, at the top of file "A," maybe.
Also, I think $_SESSION is a superglobal and is always set, so the if(!isset($_SESSION)) might do nothing (never true).
你可以这样做 $_SESSION = true;在文件“A”中,这将使 var 设置并且不会启动会话。它很脏,但确实有效。
You can do $_SESSION = true; in file "A" which would make the var set and it won't start the session. It is dirty but it does work.