【发布时间】:2009-07-10 20:44:08
【问题描述】:
<?php
// get all files from pages/ with .php extension
$pages = glob('pages/*.php');
foreach ($pages as $page) {
// remove path
$page_clean = str_replace('pages/', '', $page);
// put it in an array
$allowed_pages = array($page_clean);
// determine that the lank will be index.php?page=%
$page = $_GET['page'] . '.php';
// load page
if(in_array($page, $allowed_pages)) {
include('pages/' . $page);
} else {
echo "Page not found.";
}
}
?>
它确实包括我要求的页面,但它也回显“未找到页面”。我在这里做错了什么?
一个爱
【问题讨论】:
-
请注意,您应该使用
basename($file)而不是str_replace。 php.net/basename -
我可以说你的方法非常危险。假设您在 pages/script.php 中有一些危险代码,一个坏人只需在 Web 浏览器中键入 index.php?page=script 并且 script.php 将被自动加载。使用 if / else if 的开关,它的代码更多但更安全。
标签: php