I have a question. I have this script
<?php
$GetPage= "index";
if((isset($_GET["page"])==true) && ($_GET["page"] != "")){
$GetPage = $_GET["page"];
}
?>
But I search on stackoverflow and google. But I can't find it. I want to include a error page when php can't find the file. How can I do that? I'm jut a starter with php.
Ow almost forgoten. I use this to include a part of my site:
<?php include ("include/$GetPage.php"); ?>
Thanks for reading !
<?php
//file_exists will eliminate the need for any of your other checks.
if(file_exists($_GET["page"])){
//Set the page to be loaded if it is found on the server
$GetPage = $_GET["page"];
}else{
//Show the user a 404 error message
header("HTTP/1.0 404 Not Found");
//OR
//Set the page to be loaded as your custom error page
$GetPage = "my_error_page.php";
}
//Include the page
include $GetPage;
?>
Are you looking for a 404 redirect? Or just load a custom error page into the document? Select the above based on what you wish to do.
$GetPage = $_GET["page"]; // validate string!!
if (!file_exists('include/' . $GetPage . '.php')) {
$GetPage = 'errorPage';
}
first check file is in folder (for injection) , ($file
is full path of file.)
$path = "include";//your include folder path
if ( substr(realpath($file) , 0,strlen($path)) != $path || is_dir($file))
//error file not found
second check file is exist or not
if (!file_exists($file))
//error