我想知道如何阻止人们这样做; blahblah.com/index.php?id=../../file.php
我被告知我可以通过计算mysql行来阻止这种情况发生......?
答案 0 :(得分:1)
您只需在is_numeric
上使用PHP $_GET['id']
,或强制将id转换为数字
答案 1 :(得分:1)
你可以先检查它是否是像Nayena所说的数值,然后你可以使用MySQL请求检查这个id上的行数。如果它是1,那么它是有效的,否则请求中存在问题
编辑(示例):
<?php
if(is_numeric($_GET['id']))
{
$id = $_GET['id'];
$count = mysql_result(mysql_query("SELECT COUNT(*) FROM table WHERE id=".$id));
if($count == 1)
{
//Id is valid, moving on...
}
elseif($count == 0)
{
//Id doesn't exist, display error message
}
else
{
//Id exists more than 1 time, meaning there is a serious issue.
//Display error message or send email to webmaster
}
}
else
{
//Display error message (incorrect format for example)
}
?>
答案 2 :(得分:0)
您可以执行以下操作:
<?php
$id = isset($_GET['id']) ? (int)$_GET['id'] : false;
if ($id) {
// do your thing..
} else {
echo 'invalid item..';
}
IF $ _GET ['id']未设置,或者为0,则显示“invalid item”。 (int)也会将字符串转换为0。
另一种解决方案:
<?php
$id = isset($_GET['id']) ? $_GET['id'] : false;
if ($id && is_numeric($id)) {
// run mysql query here
} else {
echo 'Invalid item..';
}