Database Sanitize检查所有者

时间:2013-04-14 13:51:35

标签: php mysql

这是删除链接的代码:

<a href="picture_manager.php?do=delete&id=<?php print $picturedata['id']; ?>" >Delete</a>

这是我目前的数据库语法:

if (array_key_exists('do', $_GET) && $_GET['do'] == "delete" && array_key_exists('id', $_GET))
{
    $pictureid = trim(sanitize($_GET['id']));

    if ($picture->delete($pictureid) === true)
    {
        header('Location: picture_manager.php?success=removed');
    }
}

使用上面的代码,其他用户可以删除其他用户图片,如= picture_manager.php?do = delete&amp; id =(victim)。

现在我找到了防止其他用户滥用的解决方案,我更改了旧语法,如下所示:

这是我的新数据库语法:

if (!array_key_exists('id', $_GET) || $_GET['id'] == "" || $picture->pictureExists(trim(sanitize($_GET['id']))) === false || $picture->checkOwn($user->getUserID(trim(sanitize($_SESSION['key']))), trim(sanitize($_GET['id']))) === false)
{
    header('Location: picture_manager.php');
}
else    
{
    $pictureid = trim(sanitize($_GET['id']));

    if ($picture->delete($pictureid) === true)
    {
        header('Location: picture_manager.php?success=removed');
    }
}

可悲的是,它没有用“页面没有正确重定向 - 说firefox浏览器”

立即寻找专家。

我在下面的回答中找到了解决方案。

现在编辑: 当我编码如下时,对我来说很难:

if (isset($_GET['do']) && $_GET['do'] == 'delete' && (!array_key_exists('id', $_GET) || $_GET['id'] == "" || $picture->pictureExists(trim(sanitize($_GET['id']))) === false || $picture->checkOwn($user->getUserID(trim(sanitize($_SESSION['key']))), trim(sanitize($_GET['id']))) === false))
{
    header('Location: picture_manager.php');
}
else    
{
    $pictureid = trim(sanitize($_GET['id']));

    if ($picture->delete($pictureid) === true)
    {
        header('Location: picture_manager.php?success=removed');
    }
}

单击即picture_manager.php时,文件不会删除?do = delete&amp; id = 6125

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:2)

无限重定向,!array_key_exists('id',$ _GET)将始终进行。你需要添加?do = delete来验证,比如

<?php if (isset($_GET['do']) && $_GET['do'] == 'delete' && (!array_key_exists('id', $_GET) || $_GET['id'] == "" || $picture->pictureExists(trim(sanitize($_GET['id']))) === false || $picture->checkOwn($user->getUserID(trim(sanitize($_SESSION['key']))), trim(sanitize($_GET['id']))) === false))