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
    Senior Coder JamieR's Avatar
    Join Date
    Oct 2004
    Location
    United Kingdom
    Posts
    3,161
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Question Implementing error 404 element in php

    I have this PHP script which i custom which i customised for my needs:
    <?
    $id = $_REQUEST["id"];

    $arrFile[index] = "index.php";
    $arrFile[contact] = "contactme.php";
    $arrFile[poll] = "poll.php";

    if($id!="") {
    $page = $arrFile[$id];
    } else {
    $page = $arrFile[1];
    }

    include($page);
    ?>
    It gives me an array of various filenames which i can use in conjunction with linked html pages.
    What I want to know is: is it possible to implement a element into this script to let me use a 404 error page, like I do with the rest of my site when a "not found" error occurs?
    i.e. if someone tries to call /test.php?id=phpscripts, i would get this error:
    "Warning: main(): Failed opening '' for inclusion (include_path='.:/usr/local/lib/php') in /home/www/MYSITE
    /TEST.PHP on line 14" I don't want this error to occur, i want a standard 404 error page to occur. I have tried using .htaccess for this, which i used for the other error pages but to no avail.
    any ideas? :confused:

  • #2
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    make sure the array key exists in the array.. otherwise make it 404
    Code:
    if (array_key_exists($id, $arrFile)) { 
    //....
    } else {
    //404.. header('Location: http://www.domain.com/404.html');
    }
    since you're sending headers.. that should go at the top of the page before any HTML output..

  • #3
    Senior Coder JamieR's Avatar
    Join Date
    Oct 2004
    Location
    United Kingdom
    Posts
    3,161
    Thanks
    0
    Thanked 5 Times in 5 Posts
    thanks
    do i put it below the array code?
    -Jamie


  •  

    Posting Permissions

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