Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

  • #2
    New to the CF scene
    Join Date
    Feb 2010
    Location
    Texas
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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>

  • Users who have thanked F00Baron for this post:

    tomrshl (02-21-2010)

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •