簡體   English   中英

只允許帖子的EXACT用戶對其進行編輯

[英]Allow only the EXACT user of the post to edit it

假設我有一個這樣的帖子表:

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是用戶表的外鍵。

假設我可以在他登錄帳戶以執行EDIT操作(通過edit.php頁面)后使用$_SESSION['user_id']記錄用戶會話。 並假設我可以將($_GET) post_id放入超鏈接edit.php?id = 1中進行編輯。

如您所見,每個user_id都有一個特定的post_id ,並且具有唯一的登錄會話。 我的問題是,例如,當Mr.Eric (user_id #12)瀏覽自己的崗位(post_id#1) 做了把戲手動更改edit.php?id=1edit.php?id=2 ,那么他編輯post_id#2,其user_id實際上是user_id#70,這在邏輯上是不允許的

我確實嘗試了以下查詢,但是我未能按照自己的意願檢查正確的用戶會話:

$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,它必須存在並且可以編輯任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM