SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is this a good way to handle 404 error?

    Hi,

    I have the following on my site's page.php file:

    PHP Code:
    $page $_GET['page'];
    if (
    valid_page($page)) {
        echo 
    $page['page_title'];
        echo 
    $page['page_content'];
    } else {
        
    header('HTTP/1.1 404 Not Found');
        
    header('Location: /404.php');

    valid_page function checks whether the requested page exists or not in the database. Is this a good way to handle 404 errors? Do you have a better suggestion?

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The most simple way would be to create handlers in your .htaccess file that point the error documents to custom HTML files explaining the error and why they got it, apart from that i can't think of any simpler ways.

    https://www.google.com/search?q=cust...a&channel=fflb
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    The most simple way would be to create handlers in your .htaccess file that point the error documents to custom HTML files explaining the error and why they got it, apart from that i can't think of any simpler ways.

    https://www.google.com/search?q=cust...a&channel=fflb
    Hi Chris,

    I know some of the .htaccess part and I have "ErrorDocument 404 /404.php" in my .htaccess file as well. But, that alone seems not to be enough without using my PHP code. If I don't have my PHP code (which checks the existence of the page), when you type in a non-existent page, it doesn't send to 404 page, it does nothing.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Instead of redirecting to the 404 file itself you can simply include that file directly into the page and exit the script straight after.
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle


Bookmarks

Posting Permissions

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