|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2002-12-26 20:24 UTC] rasmus@php.net
[2002-12-26 20:46 UTC] antispam at theapotek dot com
[2002-12-26 21:14 UTC] rasmus@php.net
[2002-12-26 23:07 UTC] antispam at theapotek dot com
[2002-12-26 23:08 UTC] antispam at theapotek dot com
[2002-12-26 23:10 UTC] rasmus@php.net
[2002-12-26 23:11 UTC] rasmus@php.net
[2002-12-28 10:38 UTC] antispam at theapotek dot com
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 04:00:01 2025 UTC |
Calling includes or requires seems to create header output, even if the includes or requires are blank. This conflicts with session handling in situations where the session is called as a result of logic-branches in the code. I have encountered this issue on several different systems. Here's the setup. Create an include file, called "include.php". Put the following in it: <?php ?> Now, create a page with the following code: <?php include("include.php"); session_start(); session_register("SESSION"); if (!isset($SESSION)) { $SESSION["count"] = 0; print("<p>Counter initialized, please reload this page to see it increment</p>"); } else { print("<p>Waking up session $PHPSESSID</p>"); $SESSION["count"]++; } print("<p>The counter is now $SESSION[count] </p>"); ?> run the page with all the code in the browser such that it includes the include file above. Even though the include file does not write or generate a single header, the output I get is: "Warning: Cannot send session cookie - headers already sent by (output started at include.php:1) in test.php on line 4 Warning: Cannot send session cache limiter - headers already sent (output started at include.php:1) in test.php on line 4 Counter initialized, please reload this page to see it increment The counter is now 0 " My observation is corroborated by the following note, found on the session_start() function manual page at php.net. ############ As per <shadowflame at starpilot dot net>: "If you are using an include page, such as a config or possibly common-functions page, that relies on session variables being pulled from the sesssion, remember to add the session_start() function _before_ you call the includes, or your session will not be called by the page..." Any include or require called before you call session_start() will cause the initial session setup to fail, even if that include file contains only a comment! Here are scenarios: include("blank_file.php"); session_start(); This works. However: include("file_with_just_a_comment_in_it.php"); session_start(); Does not. The recommended way to use session_start() is of course to put it at the top of the page. However, if, like me, you find yourself wanting to start a session only as a result of some previous logic, this avenue is closed to you. You will have to use ob_start() and related functions, and create a code mess. It will be a beautiful day when header conflicts are caused only by the layout of my code rather than PHP code cruft. ############## As mentioned, I've encountered this on several different systems, so configure lines and php.ini doesn't seem to be too relevant. If you want build lines or php.ini, I'll send them on via e-mail.