Forum Moderators: coopster

Message Too Old, No Replies

Sending a 404 header with PHP

Is it possible?

         

Nick_W

6:30 pm on Feb 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Is it possible to use header() to send a 404? - I have the feeling it isn't and there's a gaping whole in my understanding...

If it is possible, could someone please show me how?

Many thanks

Nick

dive into perl

7:05 pm on Feb 13, 2003 (gmt 0)

10+ Year Member




<?php
header("HTTP/1.0 404 Not Found");
?>


In PHP 3, this only works when PHP is compiled as an Apache module. You can achieve the same effect using the Status header.


header("Status: 404 Not Found");

Nick_W

7:07 pm on Feb 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com] dive!

And, thanks very much, that looks just the ticket ;)

Nick

Nick_W

11:19 am on Feb 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmmm.... Neither of them work..

No, Sorry, they do work but not as expected. The page just hangs, does not show the default 404 page.

Any ideas?

Nick

stevedob

12:28 pm on Feb 15, 2003 (gmt 0)

10+ Year Member



Do a 'Location:' to a page you know doesn't exist, which should bring up your 404 page.

Nick_W

12:43 pm on Feb 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, I really need it to send the 404 from the page. And, it doesn't work, I made an error earlier saying it did...

Nick

andreasfriedrich

12:58 pm on Feb 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nick_W [webmasterworld.com] wrote at 11:19 AM on Feb. 15, 2003 in message #4 [webmasterworld.com]
Hmmmm.... Neither of them work..

No, Sorry, they do work but not as expected. The page just hangs, does not show the default 404 page.

Did you actually check the header that gets returned? I bet it had a 404 status. Remember, header() [php.net] only sends the specified header. Apache [httpd.apache.org] does not parse the headers returned by PHP [php.net] before sending out the page and it will not add your default 404 page to your output.


<?php
header("HTTP/1.0 404 Not Found");
include("/var/www/html/site.domain.com/err/404.php");
?>

This will send a 404 header() [php.net] and it will [url=http://www.php.net/include]include() [php.net][/url] your error document.

Andreas

[edited by: jatar_k at 9:46 pm (utc) on Feb. 15, 2003]

Nick_W

1:06 pm on Feb 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you're quite right. Shoudn't be programming this early in the day clearly! - Sends the 404 header just fine.

Cheers

Nick

andreasfriedrich

1:15 pm on Feb 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it working, Nick.


stevedob [webmasterworld.com] wrote at 12:28 AM on Feb. 15, 2003 in message #5 [webmasterworld.com]

Do a 'Location:' to a page you know doesn't exist, which should bring up your 404 page.

This will indeed cause Apache [httpd.apache.org] to send the 404 default error page. However, it will indicate that the resource you directed to could not be found, while sending a 404 from the original page will indicate that that resource could not be found. So your suggested solution does not achieve what Nick is after.

Andreas