开发者

can ob_start() influence included and required files?

开发者 https://www.devze.com 2023-02-08 04:25 出处:网络
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($_

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?


Disable session.use_cookies in your CLI php script via ini_set() or via php.ini configuration. This way session_start() don't try to send cookies. You have to check the cache settings for sessions as well as session_start() send HTTP headers regarding caches, too.

ini_set('session.use_cookies', 0); // disable using cookies for session ID (cookies = headers)
session_cache_limiter(false);      // disable sending cache headers
// ...
session_start();


$_SESSION are mostly based on cookie so can't work in CLI


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消