views:

1057

answers:

6

Hi there,

I'm having a problem implementing custom 404 error pages on my Windows/IIS/PHP webhost. Whenever I try to send the 404 status message in the header no content is sent to the client and a blank page is displayed instead.

Here is a very simple script I'm using:

<?php
  header('HTTP/1.1 404 Not Found');
  header('Status: 404 Not Found');
  var_dump(headers_list());
?>

On my dev machine the output (displayed in a browser) is

array(2) { [0]=>  string(23) "X-Powered-By: PHP/5.2.6" [1]=>  string(21) "Status: 404 Not Found" }

On my host there is no output. I can't seem to generate anything but a blank page regardless of what content is echoed by php, or included below as normal html, though php processing e.g. logging, database writes etc continue as normal.

Can anyone suggest what the problem might be or how to resolve it?

Thanks,

Tom

+2  A: 

What if you put text after the PHP tags?

Also, make sure your script isn't erroring out on your host, whether checking apache error logs, running php -l on your script or turning error_reporting on and display_errors on.

Matt
As you suggested, I've changed the code to:<?php error_reporting(E_ALL);ini_set('display_errors', '1');header('HTTP/1.1 404 Not Found');header('Status: 404 Not Found');var_dump(headers_list());?>CONTENTThe output is still blank on my host.
Loftx
Sorry, that didn't display too well - Also my host ran the script on the server command line for me with output: array(0) {} and no errors. Though I imagine header() is specifically for when php is invoked via a webserver.
Loftx
Try commenting out 1 line at a time until you see "CONTENT" outputted
Matt
+3  A: 

'blank page' smells like error_reporting(0) hiding the error messages you would get. Maybe there is some output before your headers and that raises an error. Check that.

zalew
+1, sounds dumb but seen this several times and should be the first thing checked
Cruachan
+1  A: 

If PHP is configured at your host to run through CGI, it may be impossible to generate 404 error pages in IIS (except by lying and returning 200 OK, which is a Bad Thing). Certainly I've been unable to persuade IIS 4-6.0 to allow my CGI 404 errors through to browsers.

You generally don't want PHP to run CGI anyway, there are other related problems as well as it being slow. On IIS, the PHP ISAPI extension should be preferred (though as I've not tried it I can't confirm it solves this specific problem).

bobince
A: 

Check that your host allows you to change headers on the fly with PHP. My host doesn't allow me to set the 404 header through PHP, though other headers (301, content-type) are fine.

DisgruntledGoat
A: 

My advice is to not set the HTTP/1.1 404 header yourself and let the web server do it for you.

Traditionally, this is done through the Status header, which the web server should take and turn into an HTTP/1.0 or 1.1 404 Not Found header.

(Side Note: The Status header is not a real HTTP header and is used solely to communicate information from an app to the server itself.)

R. Bemrose
+1  A: 

I have the same problem with PHP 5.2.6 on Linux. I find that the first 8109 bytes are not printed by the web server.

IE6 ignores custom error pages < 512 bytes in length. This could be an explanation for this problem, but probably you have tested in more than one browser anyway.