...

View Full Version : Return 404 on mistyped url variables



tomrshl
02-21-2010, 04:45 PM
I've been trying to figure out PHP and 404 errors.

If somebody types
http://site.com/?id=valid I want my PHP to deliver the content.

If they type
http://site.com/?id=invalid I want to return a 404 error and then after a couple of seconds return to the homepage.

I tried:
header("HTTP/1.0 404 Not Found"); But according to Live HTTP headers add-on in Firefox, it loads
HTTP/1.1 200 OK
first.

How can properly I deliver a 404?

Thanks a lot

F00Baron
02-21-2010, 06:18 PM
Re: the issue of not getting a 404:
Here's what I use:

header("HTTP/1.0 404 Not Found");
exit();
exit() makes sure nothing else goes out after the 404 header.
Also you must ensure that nothing gets sent to the browser before the header() call. Look for echo() and print() before the header() calls.

As to sending the user to a good page after a few seconds, I don't think that's possible with a real 404. You could send them to a real page that says 'Page not found' and add a javascript timer to send them to a better page after few seconds.

header('Location: my404.html');
exit();

The javascript would be something like:

<script type="text/javascript">
setTimeout("window.location='someBetterPage.html'",10000);
</script>

tomrshl
02-21-2010, 09:55 PM
I think, I've got it now. I think I was getting confused, the "200 OK" header was for an image file on the page. The page itself was properly returning a 404.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum