phphelpplease
phphelpplease

Reputation: 131

Creating dynamic 404 pages

I have a 404 page and I'm using php to output the page URL. My page is saved on the server as error.php, The server should be able to say to the user `Sorry Error does not exist."

Currently my code shows just the name of the page capitalised and without the extension. That's fine if the user has gone to domain.com/error.php but if the user mistypes something like domain.com/abutus I'd need the server to say Sorry Abtus does not exist.

This is what I'm working with:

<?php
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$page = basename($_SERVER['PHP_SELF']); // Get script filename without any path information
$url = str_replace( array( '.php', '.htm', '.html' ), '', $page ); // Remove extensions
$page = str_replace( array( '.php', '.htm', '.html' ), '', $url); // Remove extensions
$page = str_replace( array('-', '_'), ' ', $page); // Change underscores/hyphens to spaces
$page = ucwords( $page ); // uppercase first letter of every word
$url = ucwords( $url ); // uppercase first letter of every word

?>

<p class="404">Sorry, <?php echo ucfirst(substr($x=substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'.')),strrpos($x,'/')+1)) ; ?> does not exist.</p>

I just need to have this update based on the URL entered by the user and display that rather than the filename. It's much easier than creating and storing a new page for each possible error. I don't want to waste the server space so one dynamic page that updates would be ideal.

I've not been able to find anything relating to this so any help at all would be appreciated.

UPDATE

<?php
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$page = basename($_SERVER['REQUEST_URI']); // Get script filename without any path information
$url = str_replace( array( '.php', '.htm', '.html' ), '', $page ); // Remove extensions
$page = str_replace( array( '.php', '.htm', '.html' ), '', $url); // Remove extensions
$page = str_replace( array('-', '_'), ' ', $page); // Change underscores/hyphens to spaces
$page = ucwords( $page ); // uppercase first letter of every word
$url = ucwords( $url ); // uppercase first letter of every word

?>

<p class="404">Sorry, <?php echo ucfirst(substr($x=substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'],'.')),strrpos($x,'/')+1)) ; ?> does not exist.</p>

I've added the new code. If the user visits mydomain.com/url it doesn't echo but does with mydomain.com/url.php

UPDATE 2

<?php
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$page = basename($_SERVER['REQUEST_URI']);
?>

<p>Sorry, <?php echo ucfirst(substr($x=substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'],'.')),strrpos($x,'/')+1)) ; ?> does not exist</p>

I just need it to be able to give the URL that was typed even if the user hasn't provided an extension. It works with /page.php but if the user types /page it won't work.

Upvotes: 2

Views: 222

Answers (2)

Elementary
Elementary

Reputation: 1453

You can set a custom error page using .htaccess

ErrorDocument 404 /404.php

Then in the code you can refer to $_SERVER["REQUEST_URI"] to display the requested url and tell the user that it doesn't exist with just this code :

<?php
$x=substr($x=substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'],'.')),strrpos($x,'/')+1);
if(!$x)
    $x=substr($_SERVER['REQUEST_URI'],strrpos($_SERVER['REQUEST_URI'],'/')+1);
?>
<p class="404">Sorry, <?php echo ucfirst($x) ?> does not exist.</p>

Upvotes: 0

Alfons McBobr
Alfons McBobr

Reputation: 99

You can set a custom error page using .htaccess

ErrorDocument 404 /404.php

Then in the code you can refer to $_SERVER["REQUEST_URI"] to display the requested url and tell the user that it doesn't exist.

Upvotes: 1

Related Questions