This is not verified. I just wrote it very quickly, but it should work (I hope), and it will definitely give you the foundation to start with.
define('DEFAULT_PAGE', 'home.php');
define('ALLOWED_PAGES_EXPRESSION', '^[\/]+\.php$|^[\/]+\.html$');
function ValidateRequestedPage($p)
{
$errors_found = False;
if (preg_match('^\/.+$', $p))
{
$errors_found = True;
}
if (preg_match('^\..+$', $p))
{
$errors_found = True;
}
if (preg_match('.+\:\/\/.+', $p))
{
$errors_found = True;
}
if (!preg_match(ALLOWED_PAGES_EXPRESSION, $p))
{
$errors_found = True;
}
return !$errors_found;
}
if (!isset($_GET['page'])) { $page = DEFAULT_PAGE; }
else { $page = $_GET['page']; }
if ( !ValidateRequestedPage($page) )
{
return False;
}
require_once($page);
source
share