【问题标题】:PHP prevent URL input to delete row in databasePHP阻止URL输入删除数据库中的行
【发布时间】:2017-10-31 13:21:40
【问题描述】:

我正在开发一个博客网站,该网站的想法是当前登录的用户可以编辑和删除他们自己的帖子。我终于让它工作了,但是我的问题是如何防止用户在 URL 中写入以下输入并执行与我的 delete.php 操作相同的操作。

(示例)使用 topic_id 手动输入 URL:

/delete.php?del=133

有谁知道我可以如何编辑现有代码或知道如何更好地解决问题,我将不胜感激!

这就是我的代码的样子:

配置文件.php:

if (@$_GET['id']) {
    $check_d = mysql_query("SELECT * FROM users WHERE id ='".$_GET['id']."'");

while ($row_d = mysql_fetch_assoc($check_d)) {
        echo "<div class='spacer'></div><h2 class='headertext'>Inlägg skapade av : ".$row_d['username']."</h2>";
        $check_u = mysql_query("SELECT * FROM topics WHERE topic_creator='".$row_d['username']."' ORDER BY topic_id DESC");
        while ($row_u = mysql_fetch_assoc($check_u)) {
            $id = $row_u['topic_id'];
            echo "<tr>";
            echo "<td class='postmain'><a href='topic.php?id=$id' class='links'>".$row_u['topic_name']."<br /></a></td>";
            echo "<td class='postmain'><p class='text'>".$row_u['topic_creator']."</p><br /></td>";
            echo "<td class='postmain'><p class='text'>".$row_u['date']."</p><br /></td>";

            if($_SESSION['username'] === $row_u['topic_creator']) {
                echo "<td class='postmain'><a href='edit.php?edit=$id'><button>Redigera</button></a>";
                echo "<a href='delete.php?del=$id'><button>Ta bort</button></a></td>";
            }
            echo "</tr>";
        }
    }
}

突出显示的代码显示只有发布帖子的当前会话(用户)可以编辑和删除他们自己的帖子。

删除.php:

if (isset($_GET['del'])) {

    //getting id of the data from url
    $id = $_GET['del'];

    //deleting the row from table
    $sql = "DELETE FROM topics WHERE topic_id='$id'";
    $res = mysql_query( $sql );

    //redirecting to the display page
    header("Location:admin.php");

}

【问题讨论】:

  • 您必须在您的 delete.php 中再次检查用户是否是主题创建者。此外,由于您担心自己的安全性,您的代码容易受到 SQL 注入的影响。您应该使用准备好的语句。
  • 您已经问过类似的问题stackoverflow.com/q/45817725/1415724 并接受了答案。您发布的内容有什么问题?
  • 这个问题是相关的,但不是我在这里问的问题。我想知道如何防止 URL 输入与单击页面上的“删除”按钮时执行相同的操作。 @Fred-ii-
  • 嗨,是的,我知道准备语句。这是我的下一个优先事项。我尝试用相同的逻辑这样做,但它不起作用。当我在 delete.php 中执行 if 语句时,它根本不起作用。 // E @DimitrisFilippou
  • 首先根据登录用户检查所需的会话数组是否设置/不为空,然后使用您的if (isset($_GET['del'])) {...},这就是应该这样做的方式。 @erikos93

标签: php session get


【解决方案1】:

使用 isset 函数是这里的解决方案。 isset 函数将检查用户是否点击了删除/修改链接(即他直接在链接中粘贴了 delete.php)。所以你的代码只会在用户点击链接时执行。

if (isset($_GET['del']))
{
// your profile.php code here
}
else
{
// error message
}

【讨论】:

    【解决方案2】:

    您可以使用相同的$_SESSION 逻辑来确保访问delete.php 的任何人都具有适当的权限。

    if (isset($_GET['del'])) {
    
        //getting id of the data from url
        $id = $_GET['del'];
    
        // Get the author for the specified post to ensure they are permitted to do so
        // TODO
    
        // Check that the author is the same as the $_SESSION user
        if($_SESSION['username'] === $postAuthor) {
            //deleting the row from table - FIX THIS (see below)
            $sql = "DELETE FROM topics WHERE topic_id='$id'";
            $res = mysql_query( $sql );
        } else {
            // User is not authorized, create error handling
            // TODO
        }
    
        //redirecting to the display page
        header("Location:admin.php");
    }
    

    无关,小心 SQL 注入Bobby Tables 是一个很好的指南,您不应该使用 mysql_ 函数,而应该使用准备好的语句。

    【讨论】:

    • 抱歉,这似乎不起作用。不过谢谢你的回答! @kchason
    猜你喜欢
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2012-10-18
    • 1970-01-01
    • 2013-03-18
    相关资源
    最近更新 更多