HTTP headers and include()

HTTP headers and include()

am 10.07.2009 07:21:32 von James Colannino

Hey everyone,

I've been hard at work on a new web application, and discovered
something that I would never have seen coming. I was noticing that when
I called session_start() after a few lines of includes, I was getting
complaints because the HTTP headers had already been sent out. Then,
after putting session_start() above the include lines, suddenly
everything was working fine.

The files that were included were nothing more than functions; there was
no code executing that I could tell up to the point of the call to
session_start().

I was just wondering if anybody on the list knows why HTTP headers were
being sent out by my includes. I'm sure there's a good reason. I'm
just very curious :)

Thanks very much in advance.

James

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: HTTP headers and include()

am 10.07.2009 07:23:09 von Eddie Drapkin

On Fri, Jul 10, 2009 at 1:21 AM, James Colannino wrote=
:
> Hey everyone,
>
> I've been hard at work on a new web application, and discovered
> something that I would never have seen coming.  I was noticing that =
when
> I called session_start() after a few lines of includes, I was getting
> complaints because the HTTP headers had already been sent out.  Then=
,
> after putting session_start() above the include lines, suddenly
> everything was working fine.
>
> The files that were included were nothing more than functions; there was
> no code executing that I could tell up to the point of the call to
> session_start().
>
> I was just wondering if anybody on the list knows why HTTP headers were
> being sent out by my includes.  I'm sure there's a good reason. =C2=
=A0I'm
> just very curious :)
>
> Thanks very much in advance.
>
> James
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

HTTP headers are sent and finalized after the first bit of output. I
had the same problem before and it turned out to be because I had a
close tag "?>" at the end of a file followed by some whitespace. The
solution was to remove the ?> from the end of all the files and I
haven't closed an entire file since. Perhaps that might be it?

--Eddie

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: HTTP headers and include()

am 10.07.2009 07:49:49 von Michael Peters

James Colannino wrote:
> Hey everyone,
>
> I've been hard at work on a new web application, and discovered
> something that I would never have seen coming. I was noticing that when
> I called session_start() after a few lines of includes, I was getting
> complaints because the HTTP headers had already been sent out. Then,
> after putting session_start() above the include lines, suddenly
> everything was working fine.
>
> The files that were included were nothing more than functions; there was
> no code executing that I could tell up to the point of the call to
> session_start().
>
> I was just wondering if anybody on the list knows why HTTP headers were
> being sent out by my includes. I'm sure there's a good reason. I'm
> just very curious :)
>
> Thanks very much in advance.
>
> James
>

White space can cause this - make sure your code has top and ?> at the very bottom, or the white space may trigger the web
server to send a header and the white space as data before the cookie
for session_start() is sent.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: HTTP headers and include()

am 10.07.2009 09:24:31 von James Colannino

Eddie Drapkin wrote:

> HTTP headers are sent and finalized after the first bit of output. I
> had the same problem before and it turned out to be because I had a
> close tag "?>" at the end of a file followed by some whitespace. The
> solution was to remove the ?> from the end of all the files and I
> haven't closed an entire file since. Perhaps that might be it?

Hmm... In fact, I did close all my include files with the ?> tag, and
per Michael's observation in another response, there is a line of
whitespace after the closing tag in my include files.

I tried getting rid of the trailing whitespace, and removed the closing
tags. Unfortunately, even after that, when I place my include files
before session_start, I get the same problem. There's no leading
whitespace before the starting
It's not too big of a deal though; I simply placed my include files
after the call to session_start(). That seems to solve the problem.

James

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: HTTP headers and include()

am 10.07.2009 09:50:25 von kranthi

--005045029ff8d8fa32046e553aaa
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

a single line break after the closing ?> will not cause this problem. PHP
interpreter will neglect a single line break after ?> a good debugger like
xdebug will be helpful in this case. u can also see the source code of the
file to locate the output. any thing before php warning is the output before
session_start()

--005045029ff8d8fa32046e553aaa--

Re: HTTP headers and include()

am 10.07.2009 15:59:53 von TedD

At 12:24 AM -0700 7/10/09, James Colannino wrote:
>Eddie Drapkin wrote:
>
>> HTTP headers are sent and finalized after the first bit of output. I
>> had the same problem before and it turned out to be because I had a
>> close tag "?>" at the end of a file followed by some whitespace. The
>> solution was to remove the ?> from the end of all the files and I
>> haven't closed an entire file since. Perhaps that might be it?
>
>Hmm... In fact, I did close all my include files with the ?> tag, and
>per Michael's observation in another response, there is a line of
>whitespace after the closing tag in my include files.
>
>I tried getting rid of the trailing whitespace, and removed the closing
>tags. Unfortunately, even after that, when I place my include files
>before session_start, I get the same problem. There's no leading
>whitespace before the starting >
>It's not too big of a deal though; I simply placed my include files
>after the call to session_start(). That seems to solve the problem.
>
>James


James:

As I understand things, that's the way it is supposed to work -- you
always start a session page off with session_start() as your first
statement.

I've had some pages complain that a session has already been started
and in that case, I check to see if a session ID is set and it not,
then do a session_start().

But, as a matter of habit, I always make session_start() my first
line of code.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: HTTP headers and include()

am 10.07.2009 16:57:37 von Michael Peters

tedd wrote:
> At 12:24 AM -0700 7/10/09, James Colannino wrote:
>> Eddie Drapkin wrote:
>>
>>> HTTP headers are sent and finalized after the first bit of output. I
>>> had the same problem before and it turned out to be because I had a
>>> close tag "?>" at the end of a file followed by some whitespace. The
>>> solution was to remove the ?> from the end of all the files and I
>>> haven't closed an entire file since. Perhaps that might be it?
>>
>> Hmm... In fact, I did close all my include files with the ?> tag, and
>> per Michael's observation in another response, there is a line of
>> whitespace after the closing tag in my include files.
>>
>> I tried getting rid of the trailing whitespace, and removed the closing
>> tags. Unfortunately, even after that, when I place my include files
>> before session_start, I get the same problem. There's no leading
>> whitespace before the starting >> loss.
>>
>> It's not too big of a deal though; I simply placed my include files
>> after the call to session_start(). That seems to solve the problem.
>>
>> James
>
>
> James:
>
> As I understand things, that's the way it is supposed to work -- you
> always start a session page off with session_start() as your first
> statement.
>
> I've had some pages complain that a session has already been started and
> in that case, I check to see if a session ID is set and it not, then do
> a session_start().
>
> But, as a matter of habit, I always make session_start() my first line
> of code.
>
> Cheers,
>
> tedd
>

If the included file has

somefunc() {
}
?>

somefunc() {
}
?>

that will also cause it.
Or maybe one of the include files includes a file (IE db connection
script) that has white space.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: HTTP headers and include()

am 12.07.2009 13:01:12 von 9el

--00163646c94ace8612046e8020f9
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

On Fri, Jul 10, 2009 at 12:49 PM, Michael A. Peters wrote:

> James Colannino wrote:
>
>>

> White space can cause this - make sure your code has > and ?> at the very bottom, or the white space may trigger the web server to
> send a header and the white space as data before the cookie for
> session_start() is sent.



Well, at the end of the file dont put a ?> on your php files especially
which files you are gonna include at others. It would save you some
dreadfull buggy times. Its also practiced in the framework programming.

--00163646c94ace8612046e8020f9--

Re: HTTP headers and include()

am 12.07.2009 13:09:42 von Ashley Sheridan

On Sunday 12 July 2009 12:01:12 Lenin wrote:
> On Fri, Jul 10, 2009 at 12:49 PM, Michael A. Peters wrote:
> > James Colannino wrote:
> >
> >
> >
> > White space can cause this - make sure your code has > > top and ?> at the very bottom, or the white space may trigger the web
> > server to send a header and the white space as data before the cookie for
> > session_start() is sent.
>
> Well, at the end of the file dont put a ?> on your php files especially
> which files you are gonna include at others. It would save you some
> dreadfull buggy times. Its also practiced in the framework programming.

Well, some frameworks insist on it being in there. I tend to always include
them, but I use a text editor that I know won't add characters to the end
after the final ?>

--
Thanks,
Ash
http://www.ashleysheridan.co.uk

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: HTTP headers and include()

am 12.07.2009 14:08:36 von TedD

At 12:09 PM +0100 7/12/09, Ashley Sheridan wrote:
>On Sunday 12 July 2009 12:01:12 Lenin wrote:
>> On Fri, Jul 10, 2009 at 12:49 PM, Michael A. Peters wrote:
>> > James Colannino wrote:
>> >
>> >
>> >
>> > White space can cause this - make sure your code has >> > top and ?> at the very bottom, or the white space may trigger the web
>> > server to send a header and the white space as data before the cookie for
>> > session_start() is sent.
>>
>> Well, at the end of the file dont put a ?> on your php files especially
>> which files you are gonna include at others. It would save you some
>> dreadfull buggy times. Its also practiced in the framework programming.
>
>Well, some frameworks insist on it being in there. I tend to always include
>them, but I use a text editor that I know won't add characters to the end
>after the final ?>
>
>--
>Thanks,
>Ash

Ash:

I do the same. It would brother me (lack of symmetry) if I didn't
include a ?> at the end of my scripts.

As for additional characters after the ?>, I make sure my
terminations don't have any. Just because characters can be white
space doesn't mean you can't detect them.

Cheers,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: HTTP headers and include()

am 12.07.2009 18:04:39 von zareef ahmed

--0015174c3f4cdaf609046e845ccf
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

On Fri, Jul 10, 2009 at 12:54 PM, James Colannino wrote:

> Eddie Drapkin wrote:
>
> > HTTP headers are sent and finalized after the first bit of output. I
> > had the same problem before and it turned out to be because I had a
> > close tag "?>" at the end of a file followed by some whitespace. The
> > solution was to remove the ?> from the end of all the files and I
> > haven't closed an entire file since. Perhaps that might be it?
>
> Hmm... In fact, I did close all my include files with the ?> tag, and
> per Michael's observation in another response, there is a line of
> whitespace after the closing tag in my include files.
>
> I tried getting rid of the trailing whitespace, and removed the closing
> tags. Unfortunately, even after that, when I place my include files
> before session_start, I get the same problem. There's no leading
> whitespace before the starting >
> It's not too big of a deal though; I simply placed my include files
> after the call to session_start(). That seems to solve the problem.
>

That's a good practice, (Although not optimal is some application setups )
but I am wondering why you are not getting information about exact place
where output is being started.
You should get a "headers already sent output started at ...." kind of error
if you have enabled error reporting with display_errors ON.


>
> James
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Zareef Ahmed :: A PHP Developer in India ( Delhi )
Homepage :: http://www.zareef.net

--0015174c3f4cdaf609046e845ccf--

Re: HTTP headers and include()

am 12.07.2009 19:57:43 von James Colannino

Zareef Ahmed wrote:

> You should get a "headers already sent output started at ...." kind of error
> if you have enabled error reporting with display_errors ON.

Actually, I did. I just didn't think to mention it in my first post.
The thing was that it said it was coming from one of my includes, even
though I wasn't yet printing anything to the browser. That's why I was
so confused.

I've been following what tedd said in an earlier post (to make
session_start() your first line of code) and haven't had a problem since.

James


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: HTTP headers and include()

am 12.07.2009 20:17:42 von zareef ahmed

--0015174c1aacaaee4e046e86388f
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

On Sun, Jul 12, 2009 at 11:27 PM, James Colannino wrote:

> Zareef Ahmed wrote:
>
> > You should get a "headers already sent output started at ...." kind of
> error
> > if you have enabled error reporting with display_errors ON.
>
> Actually, I did. I just didn't think to mention it in my first post.
> The thing was that it said it was coming from one of my includes, even
> though I wasn't yet printing anything to the browser. That's why I was
> so confused.


Its not only print or echo command which may output.
As Michael was pointing even a space can cause this problem.
Your error message will tell you about exact location of the problem spot
with line number.

(If you are only getting this problem after uploading your code to server
then your FTP client may be culprit)


>
>
> I've been following what tedd said in an earlier post (to make
> session_start() your first line of code) and haven't had a problem since.
>

Yes, its a good practice for almost all applications and you should do it
always unless you have a reason to do otherwise.
If its really hard to maintain (like working with older codebase with lots
of references to session_start in the middle of application process) then
just putting ob_start at very start of application can also solve the
problem.


> James
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Zareef Ahmed :: A PHP Developer in India ( Delhi )
Homepage :: http://www.zareef.net

--0015174c1aacaaee4e046e86388f--