在我的页面上,用户可以选择显示什么数据。我只是使用下拉列表和GET参数来完成这项工作。目前看起来是这样的:
形式:
<form method="get" action="contents.php">
<select name="TutorialBlock">
<option value="tuts1">Block One - Starting</option>
<option value="tuts2">Block Two</option>
<option value="tuts3">Block Three</option>
</select>
<input type="submit">
</form>
根据用户选择的选项加载数据的脚本:
<?php
$var_value = $_GET['TutorialBlock'];
include '/includes/'.$var_value.'.php';
?>
这很好,PHP根据用户选择的选项包括正确的文件,问题是,如果用户没有选择选项,PHP只会抛出未找到的文件错误,因为它正在寻找一个不存在的文件。如果没有设置GET参数,有没有办法阻止PHP脚本运行?
您现在所做的事情导致了一些严重的漏洞。你永远不能相信用户的输入。
您应该针对白名单运行$_GET['TutorialBlock']
。这是一个给你的例子。
$whitelist = array(
'page',
'blockpage',
//....etc
);
if(isset($_GET['TutorialBlock']) && !empty($_GET['TutorialBlock'])) {
// now make sure it's in the whitelist.
if(!in_array($_GET['TutorialBlock'], $whitelist)) {
die('bad page');
} else {
include '/includes/'.$_GET['TutorialBlock'].'.php';
}
} else {
// user didn't select page... do something here..
}
以上只是伪代码(示例(,您仍然需要确保用户输入是有效的
$var_value = isset($_GET['TutorialBlock']) ? $_GET['TutorialBlock'] : false;
if($var_value) {
include '/includes/'.$var_value.'.php';
} else {
// query value wasn't there
exit("TutorialBlock is required");
}
重要
您的代码很容易受到目录遍历攻击。
if(isset($_GET['TutorialBlock'])) {
$var_value = $_GET['TutorialBlock'];
include '/includes/'.$var_value.'.php';
} else {
//not set
}