2015-07-03 105 views
0

我有一個PHP腳本,允許用戶註冊條目到數據庫。條目是自動增加的。我發現,用戶#A可以通過將url從edit.php?id = 2更改爲id = 1來從用戶#B獲得條目。防止用戶編輯其他條目

我當然希望防止。所以我的想法是:如果mysql條目中的用戶ID字段與我的php腳本中的$ _SESSION ['user_id']匹配,則允許編輯。

用戶應該只能夠編輯他們已經貼出自己的條目。

什麼將是實現這一目標的最佳和最有效的方法是什麼?

<?php $bruker = $_SESSION['user_id']; ?> 

<?php } 
/* 
    EDIT RECORD 
*/ 
// if the 'id' variable is set in the URL, we know that we need to edit a record 
if (isset($_GET['id'])) 
{ 
    // if the form's submit button is clicked, we need to process the form 
    if (isset($_POST['submit'])) 
    { 
     // make sure the 'id' in the URL is valid 
     if (is_numeric($_POST['id'])) 
     { 
      // get variables from the URL/form 
      $id = $_POST['id']; 
      $elv = htmlentities($_POST['elv'], ENT_QUOTES); 
      $vald = htmlentities($_POST['vald'], ENT_QUOTES); 
      $art = htmlentities($_POST['art'], ENT_QUOTES); 
      $dato = htmlentities($_POST['dato'], ENT_QUOTES); 
      $vekt = (int)$_POST['vekt']; 
      $lengde = (int)$_POST['lengde']; 
      $flue = htmlentities($_POST['flue'], ENT_QUOTES); 
      $gjenutsatt = (int)$_POST['gjenutsatt']; 
      $kjonn = (int)$_POST['kjonn']; 
      $bilde = htmlentities($_POST['bilde'], ENT_QUOTES); 
      $user = $_SESSION['user_id']; 

      // check that required fields are not empty 
      if ($elv == '' || $vald == '' || $art == '' || $dato == '' || $vekt == '' || $kjonn == '') 
      { 
       // if they are empty, show an error message and display the form 
       $error = 'Du må fylle ut de påkrevde feltene!'; 
       renderForm($elv, $vald, $art, $dato, $vekt, $lengde, $flue, $gjenutsatt, $kjonn, $bilde, $user, $error, $id); 
      } 
      else 
      { 
       // if everything is fine, update the record in the database 
       if ($stmt = $mysqli->prepare("UPDATE fisk SET elv = ?, vald = ?, art = ?, dato = ?, vekt = ?, lengde = ?, flue = ?, gjenutsatt = ?, kjonn= ?, bilde = ?, user = ? 
        WHERE id=?")) 
       { 
        $stmt->bind_param("ssssiisiisii", $elv, $vald, $art, $dato, $vekt, $lengde, $flue, $gjenutsatt, $kjonn, $bilde, $user, $id); 
        $stmt->execute(); 
        $stmt->close(); 
       } 
       // show an error message if the query has an error 
       else 
       { 
        echo "ERROR: could not prepare SQL statement."; 
       } 

       // redirect the user once the form is updated 
       header("Location: /"); 
      } 
     } 
     // if the 'id' variable is not valid, show an error message 
     else 
     { 
      echo "Error!"; 
     } 
    } 
    // if the form hasn't been submitted yet, get the info from the database and show the form 
    else 
+0

'選擇數據,你需要從thetable其中的recordId = $ foo和用戶ID = $ currentuser'。如果它不是合適的用戶,他們可以破解他們想要的url,他們將永遠不會獲得任何記錄數據進行編輯。 –

+0

另外,當您嘗試編輯數據以防止普通人混淆輸入時,您應該避免使用GET字段 – MiltoxBeyond

回答

0

假設你的用戶有唯一的ID,你可以簡單地添加額外的WHERE子句的SQL:

如果($語句= $ mysqli->準備(「UPDATE菲斯克SET ELV =,VALD =?,art = ?, dato =?,vekt =?,lengde =?,煙道=?,gjenutsatt =?,kjonn =?, bilde =?,user =? WHERE id =?AND created_user =?「 ))

顯然取代created_user與您用於存儲創建該條目的用戶標識的列。

只會不斷更新用戶嘗試編輯它創建的行的方式。

更安全的是,您可以通過首先查詢相關行的創建用戶標識,然後根據您的用戶標識$ _SESSION檢查它,然後查殺腳本或將它們重定向到它之前永遠得到查詢。

+0

如果他們不是首先創建它,我應該阻止他們看到該頁面。否則,表單將返回其他用戶的內容。所以我會在第一個if語句附近添加一個查詢? –

+0

在頁面頂部,檢查他們的授權編輯該項目 - 如果沒有發送或殺死它 – JohnnyFaldo

相關問題