Loftx
Loftx

Reputation: 1792

PHP 404 error page won't display content

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?

Upvotes: 0

Views: 3757

Answers (6)

max
max

Reputation:

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.

Upvotes: 1

Powerlord
Powerlord

Reputation: 88786

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.)

Upvotes: 0

DisgruntledGoat
DisgruntledGoat

Reputation: 72510

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.

Upvotes: 0

bobince
bobince

Reputation: 536329

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).

Upvotes: 2

zalew
zalew

Reputation: 10311

'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.

Upvotes: 5

Matt
Matt

Reputation: 2230

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.

Upvotes: 2

Related Questions