if (file_exists("pages/$page.php")) {
include($page.'.php');
}
Run Code Online (Sandbox Code Playgroud)
这样安全吗?
使用Safe我的意思是你不能包括远程脚本等
当然不是,特别是如果 $page = "./configuration"
我建议用这样的东西替换它:
$pages = array("blog", "home", "login");
if (in_array(strtolower($page), $pages))
include("pages/$page.php");
Run Code Online (Sandbox Code Playgroud)
编辑:您可以使用此代码生成可接受页面的列表.
$pages = array();
if ($dh = opendir("pages")) {
while (($file = readdir($dh)) !== false) {
if (strstr($file, ".php") !== false) // make sure it is a .php file
$pages[] = substr($file, -4); // remove the .php
}
closedir($dh);
}
Run Code Online (Sandbox Code Playgroud)