Re: [PHP3] can sessions & ocilogon be inside a require() file?

From: Date: Tue, 20 Jun 2000 22:29:53 +0000
Subject: Re: [PHP3] can sessions & ocilogon be inside a require() file?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-2385@lists.php.net to get a copy of this message
In article <39494E27.DA8D9789@stromix.com>, paul@stromix.com (Paul Fernandez) wrote: > In an effort to optimize, I took this and put it into a require file: > > <? > session_start(); > Are there exceptions to Require() prohibiting this? No. require almost literally copies and pastes the one file into the other. Only trick is this: If one of the files you required before this one has some whitespace (like a newline at the end, after the ?>) then the headers got sent along with that content (the newline) and the cookie headers can't be sent, cuz it's too late... Or if there's a space or newline before the <?php in the file itself Or if you do an echo before the session_start() Or... Oh hell. Let me explain why all these things are verboten: What a browser *REALLY* sees is something not unlike this: (not including the dashed lines, and vaugely speaking) ------------------------------------------- Content-type: text/html Content-length: 1234 Cookie: foo=1 12:34:56 06 Jun 2000 GMT Other-headers: whatever your little heart desires <HTML> <BODY>whatever</BODY> </HTML> ------------------------------------------- Notice that blank line between the headers and the <HTML>. That's the signal for the "end of headers, here comes the content" to the browser. Everything after the blank line is considered content. Now, these lines are being streamed from PHP to the browser as it goes, more or less. So when you have your first space, newline, or <TAG> or echo/print in PHP, PHP has to send the headers, and then a newline to signify the end of headers, and then start the actual content, which is what it thinks you want, since you said so, and it's not a header, so it must be content. Once PHP does that, there really ain't no way it can call it back and add more headers: They done went out already. So when you have a newline after a ?> in an include file, before a call to session_start() or header() or setcookie(), here's what PHP tries to send: ------------------------------------------- Content-type: text/html Content-length: 1234 Cookie: foo=1 12:34:56 06 Jun 2000 GMT Other-headers: whatever your little heart desires <HTML> <BODY>whatever</BODY> </HTML> ------------------------------------------- Those extra blank lines in there after the Content-length are caused by *YOU* having a blank line that PHP assumes is content (cuz it sure ain't a header), and so PHP sends blank line to end the headers, and then your blank line that's part of your content, and then... You try to send another header. But PHP is already past the headers and into the content, so PHP just plain can't do that, so it pretty much has to quit and give you an error message. -- Richard Lynch | If this was worth $$$ to you, buy a CD US Customer Support Director | from one of the artists listed here: Zend Technologies USA | http://www.L-I-E.com/artists.htm http://www.zend.com | (this has nothing to do with Zend, duh!)

« previous php.general (#2385) next »