Forum Moderators: coopster

Message Too Old, No Replies

headers already sent

         

smallcompany

4:53 pm on Mar 18, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For years, I've been using PHP to email 404 errors. Since recently, on one of the sites, this message started being logged every time 404 was triggered:

[18-Mar-2015 12:08:04 America/New_York] PHP Warning: Cannot modify header information - headers already sent by (output started at /path/error-404.php:1) in /path/error-404.php on line 2

PHP code is at the very top of 404 custom file, above the HTML code:

<?php 
header('Status: 404 Not Found', null, 404);
$from_header = "From: 404@example.com\r\n";
$to = "404@example.com";
$subject = "404 Error";
$today = date("D M j Y g:i:s a T");
$ip = getenv ("REMOTE_ADDR");
$requri = getenv ("REQUEST_URI");
$servname = getenv ("SERVER_NAME");
$pageload = $ip . " tried to load http://" . $servname . $requri ;
$httpagent = getenv ("HTTP_USER_AGENT");
$httpref = getenv ("HTTP_REFERER");
$message = "$today \n\n$pageload \n\nUser Agent = $httpagent \n\n$httpref ";
mail($to, $subject, $message, $from_header);
?>


I have another website with the same hosting company, same package, different server. No such errors are being produced, nor I get them on other sites hosted with other companies.

I wonder about how to troubleshoot this.

Thanks

smallcompany

5:05 pm on Mar 18, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I wonder about how to troubleshoot this.

Continue searching the web.

It was about how the file was saved, that is, encoded.

When I compared the two files in Notepad++, the "good" one was set as "Encode in UTF-8 without BOM" while the "offender" was just "Encode in UTF-8"

I found a hint here: [php.net...] (where else about PHP, eh?)

Anyway, yet another case of rush to a forum. Still, without rushing, there would be no this post which I hope may help some in the future.

Cheers!

lucy24

6:58 pm on Mar 18, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The underlying lesson is: Instruct all your text editors not to use a BOM ever, because there's honestly no need for it and it can do more harm than good :)