the thing is even though your checking for bad stuff, your still betting you have considered all possibilites. the hacker may easily know a few more tricks than you do.
if you MUST construct your include using the GET variable directly, this would be pretty safe, as long as you keep php files you dont want included out of the pages/ dir
it will prevent ../../config.php type attacks
$include_dir = 'pages/';
$include_file = $include_dir.'/'.$_GET['page'] . '.php';
if ($include_dir === dirname($include_file)) {
if (is_readable($include_file) && is_file($include_file)) {
include($include_file);
}
}
but the BEST thing is too simply NOT allow ANYTHING you have not SPECIFICALLY allowed, like this
$available_files = array(
'homepage.php',
'page1.php',
'path/to/page2.php',
'static.html',
);
$include = 'homepage.php'; // default
if (isSet($_GET['page'])) {
if (in_array($_GET['page'], $available_files)) {
$include = $_GET['page'];
}
}
if (is_readable($include)) {
include ($include);
} else {
exit('page not avail, the webmaster forgot to upload the file even though he defined it as an available file lol');
}