0

My Hompeage is loading the sites with this script:

<?php
            if(!isset($_GET['id'])) $id = 'home';
            else $id = $_GET['id'];
            include($id.'.php');
        ?>   

Does anybody of you have an idea, how to connect a 404 error page with it? Means, if Php can't find the page it will send the user to a 404 page.

Thanks for your help!

JSt
  • 799
  • 2
  • 10
  • 21
  • What server are you using ? apache using .htaccess does it very easily [http://www.htaccessbasics.com/404-custom-error-page/](http://www.htaccessbasics.com/404-custom-error-page/) – Adidi Mar 24 '13 at 16:31
  • 1
    Please be very careful with this code as it seems to be very dangerous. Basically, you're allowing the user to take full control over your files. Consider the situation where the user is able to pass something like this as 'id': `'../../../some-file'`. You should really check if the user is allowed to access that file and its best to strip path information from the passed argument – thaJeztah Mar 24 '13 at 16:37
  • I checked, but you really can just get the php files and can't access other folders. – JSt Mar 24 '13 at 17:05

2 Answers2

1

If you want to redirect the user because of non-existent file, you should uses directives of the server, in Apache it is in the .htaccess file.

If you want to redirect the user because of the non-existing id so it would like non=existent file you can redirect him:

if($_GET['id']) ... is not existent ){ // type your criteria here
  header('Location: your_404_page.php');
  die();
}
Voitcus
  • 4,463
  • 4
  • 24
  • 40
0

Something like this maybe?

<?php

if( !isset( $_GET['id'] ) )
{
    $id = 'home';
}
else
{
    $id = $_GET['id'];
}

if ( is_file( $id . '.php' ) )
{
    include( $id . '.php' );
}
else
{
    include( '404.php' );
}

?> 
Andreas Hagen
  • 2,335
  • 2
  • 19
  • 34