Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-21-2010, 03:45 PM   PM User | #1
tomrshl
New Coder

 
Join Date: Aug 2009
Posts: 31
Thanks: 3
Thanked 0 Times in 0 Posts
tomrshl is an unknown quantity at this point
Return 404 on mistyped url variables

I've been trying to figure out PHP and 404 errors.

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

If they type
Code:
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:
Code:
header("HTTP/1.0 404 Not Found");
But according to Live HTTP headers add-on in Firefox, it loads
Code:
HTTP/1.1 200 OK
first.

How can properly I deliver a 404?

Thanks a lot
tomrshl is offline   Reply With Quote
Old 02-21-2010, 05:18 PM   PM User | #2
F00Baron
New to the CF scene

 
Join Date: Feb 2010
Location: Texas
Posts: 8
Thanks: 0
Thanked 3 Times in 3 Posts
F00Baron is an unknown quantity at this point
Re: the issue of not getting a 404:
Here's what I use:
PHP Code:
    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.
PHP Code:
header('Location: my404.html');
exit(); 
The javascript would be something like:
Code:
<script type="text/javascript">
setTimeout("window.location='someBetterPage.html'",10000);
</script>
F00Baron is offline   Reply With Quote
Users who have thanked F00Baron for this post:
tomrshl (02-21-2010)
Old 02-21-2010, 08:55 PM   PM User | #3
tomrshl
New Coder

 
Join Date: Aug 2009
Posts: 31
Thanks: 3
Thanked 0 Times in 0 Posts
tomrshl is an unknown quantity at this point
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.
tomrshl is offline   Reply With Quote
Reply

Bookmarks

Tags
404

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:51 AM.


Advertisement
Log in to turn off these ads.