阻止插入到数据库中的直接 URL 操作


Block Direct URL action that inserts into DB

我在文件视图中有一个操作报告它是关于票证的:

if(isset($_GET['closeticket']) == 'true')
{
$db->query("update tickets set status='Closed' where id='$id'");
header("Location: viewreport?id=".$id."");

但即使是用户也可以通过 url 关闭不属于他的票证。所以我想阻止直接 url 操作。

这是动作

a href "viewreport?closeticket=true&id= <?php echo $id;?>" class="btn btn-danger" id="">Close</a>

您应该通过会话或 cookie 检查此操作是否属于用户。

一定是这样的

if($_SESSION["group"] == "Admin" ){
 // update operation.
}

我希望这对你有帮助。

在执行之前,您应该检查是否允许用户关闭该报告。

因此,像这样:

if(isset($_GET['closeticket'])) 
{
    $userIsAllowed = true; // your magic here
    if ($userIsAllowed) {
        $db->query("update tickets set status='Closed' where id=" . $db->quote($id));
        header("Location: viewreport?id=".$id."");
    } else {
        echo "You're not allowed closing this ticket";
    }
}

确保正确转义评论中提到的查询(由切尔默茨)