Skip to main content

Custom 404 page - PHP


I have a custom 404 page which works fine except for the message I want to display on this page.



I would like it to say the url of the page which can't be found but instead it displays the url of the 404 page.



Here's what I have...



You were looking for <?php echo $_SERVER['REQUEST_URI'] ?>.



The htaccess file contains the line: ErrorDocument 404 /404/


Source: Tips4allCCNA FINAL EXAM

Comments

  1. You need to use $_SERVER['HTTP_REFERER'] instead - that will be the address they requested first.

    This only works in the exact case described in the question - where the browser has actually been redirected to the 404 page. In that situation, $_SERVER['REQUEST_URI'] contains the URI of the 404 page rather than the originally requested page as described.

    Using Apache's ErrorDocument 404 /handle404.php in the site config or .htaccess would mean that $_SERVER['REQUEST_URI'] would actually work, but a more robust solution is the option in the update below.

    Update:

    Apparently $_SERVER['REDIRECT_URL'] might be a better bet however, having searched around a bit.

    For both cases, as mentioned by the commenters below, bear in mind that any headers are just as prone to malicious content as $_POST, $_GET and others, so process them before outputting anything.

    Update 2:

    Didn't see the post from @Janoz below - he correctly mentions REDIRECT_URL.

    ReplyDelete
  2. From the perspective of the php page, that really is the request uri. Showing the error page is done by the webserver. Apache for example will add some extra server variables. REDIRECT_URL is probably the one you are looking for.

    ReplyDelete
  3. If a page doesn't exist you redirect him to the 404 page? Idealy, I would display the 404 directly on the page which wasn't found. This way, you don't have to redirect, and you can correctly use REQUEST_URI. And the code for your 404 can still be centralized!

    ReplyDelete
  4. use file_exists to check whether the file your user is looking for exists or not. if it doesn't the redirect them to a custom made error page.

    ReplyDelete
  5. I did not write this function but it is what I use to do the same thing:

    function selfURL() {
    $s = empty($_SERVER["HTTPS"]) ? ''
    : ($_SERVER["HTTPS"] == "on") ? "s"
    : "";
    $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
    $port = ($_SERVER["SERVER_PORT"] == "80") ? ""
    : (":".$_SERVER["SERVER_PORT"]);
    return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
    }
    function strleft($s1, $s2) {
    return substr($s1, 0, strpos($s1, $s2));
    }


    then to print it:

    <?php print(selfURL()); ?>

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.