NYCPHP Meetup

NYPHP.org

[nycphp-talk] warning message on the php page

Dan Cech dcech at phpwerx.net
Tue Feb 5 20:11:24 EST 2008


chad qian wrote:
> I try to debug one php page.Everything is fine but I always get this
> error message on top when I load the web page:
> 
> Warning: session_start() [function.session-start]: Cannot send
> session cache limiter - headers already sent (output started at
> /home/getatwoq/public_html/chad/registrationverified.php:3) in
> /home/getatwoq/public_html/chad/inc/prehtml.php on line 23

All,

I have been following the comments in this thread with surprise.  This 
is a fairly simple problem and should really be part of PHP-101.

The output is being started on line 23 of prehtml.php.  Odds are fairly 
good that this file is 23 lines long and the end of the file looks like 
this:

?>[\n]

That trailing newline after the closing PHP tag is causing output to 
start and resulting in the error message above.

In general, 2 simple rules for include file will save you these kind of 
headaches.

1. Begin the file with <?php, don't put any whitespace before it.  This 
is pretty obvious and the usual way of doing things.

2. End the file with a comment, like:

// end of file[\n]

I recall this concept being explained to me on this very list, the gist 
of it is that the php parser will stop when it hits the end of the file, 
so the explicit ?> tag is redundant.  Most editors however will add a 
newline [\n] at the end of the file which is then sent to the browser 
and breaks session and header functions.

The problem is not in any way related to whitespace within the PHP tags, 
as this is not sent to the browser, it is the whitespace outside the PHP 
tags (like a newline following the PHP end tag) that causes problems. 
By not explicitly closing the PHP tag the whitespace will all be treated 
as PHP code and ignored by the parser.

Follow these simple rules and you will not have to worry about this 
problem any more.

Dan



More information about the talk mailing list