只允许帖子的EXACT用户编辑它


Allow only the EXACT user of the post to edit it

假设我有一个这样的posts表:

post_id       topic_id       user_id     content         date
 1               7            12         bla bla         7/15/2014
 2               16           70         bla bla         7/16/2014
 3               7            14         bloa bla        7/20/2014

user_id为users表的外键。

假设我可以在他登录到他的帐户执行EDIT操作后使用$_SESSION['user_id']记录用户会话(通过EDIT .php页面)。假设我能把($_GET)和post_id放到超链接edit。php中?编辑Id =1

可以看到,每个user_id都有一个特定的post_id和唯一的登录会话。我的问题是,例如,当Mr.Eric (user_id #12)浏览他自己的帖子(post_id#1) 通过手动将edit.php?id=1更改为edit.php?id=2来做一个技巧,然后他编辑post_id #2,其user_id实际上是user_id#70,这是NOT ALLOWED逻辑。

我确实尝试了以下查询,但未能按我希望的那样检查正确的用户会话:

$q="SELECT user_id, post_id, content
        FROM posts
        WHERE user_id = {$_SESSION['user_id']}
        ";
$r = mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$uid = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : -1;
$current_user = $row['user_id'];
}
if(!$uid && $uid != $current_user){
   echo 'sorry, but this is not your own post for editing!';
}
你能帮帮我吗?谢谢你!

看起来if(!$uid && $uid != $current_user){中的&&应该是||

这行看起来像它在伪代码中所说的if $uid doesn't exist AND $uid does not equal $current user do something, otherwise, do nothing。$uid确实存在于您的示例中,因此它将返回FALSE并跳过整个if

这意味着$uid不必匹配$current_user,它只需要存在,它可以编辑任何东西