Forum Moderators: coopster

Message Too Old, No Replies

Headers already sent

PHP reports "headers already sent" when they should not have been sent

         

ellipsis

2:54 am on Sep 18, 2009 (gmt 0)

10+ Year Member



I'm getting the "Warning: Cannot modify header information - headers already sent by (...)" error.

It is occurring on my index page - it's the first page loaded, it's not undergoing a redirect.

I've tried including the ob_start statement, although my setcookie was the very first command anyway - I even tried removing my .htaccess file, if that was causing any headers.

The file is plain PHP, starting at Line 1 column 1 (or so reports Notepad++), reading:

<?php
ob_start();
if(empty($_COOKIE['popup'])){
setcookie ("popup", "1", time()+604800);
//echo 'i set the cookie'; //debug only
}
[i][... load of PHP, completely enclosed in the PHP tags...][/i]
ob_end_flush();
?>

Now, I started to go crazy, so I opened the PHP file in my online file-editor management tool (my host runs Plesk) - and it reports the file looks like:

&#65279;<?php
ob_start();
if(empty($_COOKIE['popup'])){
setcookie ("popup", "1", time()+604800);
//echo 'i set the cookie';
}
[... yada yada yada...]

Where did the little "dot" come from (it appears as a dot/bullet in my file viewer, not as the HTML entity)?

Details of host (no control over this):
Apache VersionApache/2.2.3 (Debian) mod_jk/1.2.18 mod_python/3.2.10 Python/2.4.4 PHP/4.4.4-8+etch4 mod_ssl/2.2.3 OpenSSL/0.9.8c mod_perl/2.0.2 Perl/v5.8.8

Cheers-
Daniel

Tommybs

6:41 am on Sep 18, 2009 (gmt 0)

10+ Year Member



Too me it looks like this is either a problem with the editor you have used, or an issue with how the file was uploaded. You need to remove the "dot" so that the php tag is the first thing (with no blank spaces or lines) followed by ob_start(); Can you just delete it in the file viewer?

Sorry I can't offer a better explanation but that is all I can think of

ellipsis

6:50 am on Sep 18, 2009 (gmt 0)

10+ Year Member



Thanks for the reply-
I can see the "dot" when I view the file, but when I edit it, even using the online editor, it isn't there.

It appears to be some hidden character... my text editor is in UTF8 - but the error is happening before the DOCTYPE encoding would even be sent to the browser... there is some file output, that's hidden, right at the start of my file that text-editors ignore, but readers output.

Tommybs

7:31 am on Sep 18, 2009 (gmt 0)

10+ Year Member



I would just suggest opening the file in notepad and try deleting the first line and adding it in again. Also try searching for FTP upload formats and see what encoding you should use (sorry can never remember that bit) then see if you can change your FTP client over

rocknbil

3:50 pm on Sep 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard ellipsis!

Couple possibilities:

&#65279

Google "&#65279 PHP" and the first thread is right here [webmasterworld.com]..

Byte order mark? (BOM)

Uploading in ASCII format? Do you have command line access on your server, and can edit in nano, pico, etc.? Open it and resave it, does it work?

Windows and Linux end of line characters are different. I have all sorts of issues with Notepad edited files in various Linux servers for this reason. - perl scripts often will not run unless saved in Unix format. There is no specific error, it's just premature end of script headers.

I use HomeSite, and save all files in Unix format prior to deployment. This is the same reason some people see "little black boxes" where line returns should be when editing my files.

So if all else fails, get a good editor that supports Unix format when saving files, this has eliminated a lot of problems for me.

ellipsis

9:58 pm on Sep 18, 2009 (gmt 0)

10+ Year Member



A-ha! Thank you rocknbil - my editor even had a encoding option of "UTF without BOM" which worked fine.

I did previously search google, but I searched for &#65279; - the semi colon gave me zero results! Sneaky buggers. Thanks again.