2017-10-31 7 views
-1

私は、現在ログインしているユーザーが自分の投稿を編集して削除できるというアイデアがあるブログWebサイトで作業しています。私はついにそれを動作させましたが、私の質問は、ユーザーがURLに次の入力を書き込んで、私のdelete.phpアクションと同じアクションを実行できないようにする方法です。 topic_idとPHPはURL入力によるデータベース内の行の削除を防ぎます

(例)マニュアルURL入力:

/delete.php?del=133

私は私の既存のコードを編集したり、私は意志の問題へのよりよい解決策を知ることができますどのように誰もが知っていますか大変感謝しています!

これは私のコードがどのように見えるかです:ハイライトされたコードはポストを作っただけで、現在のセッション(ユーザ)が編集できることを示して

enter image description here

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>"; 
     } 
    } 
} 

と:

Profile.php自分の投稿を削除します。

Delete.php:

enter image description here

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"); 

} 
+0

あなたのdelete.phpに再びトピック作成者かどうかをチェックする必要があります。また、セキュリティについて心配しているので、コードはSQLインジェクションに対して脆弱です。 Prepared Statementsを使用する必要があります。 –

+0

あなたはすでに同様の質問をhttps://stackoverflow.com/q/45817725/1415724に尋ねて答えを受け入れました。あなたが投稿したものでうまくいかないのは何ですか? –

+0

その質問は関連していますが、私がここで求めているものと同じではありません。私は私のページ上の "削除"ボタンをクリックしたときと同じことをURL入力から防ぐ方法を知りたい。 @ Fred-ii- – erikos93

答えて

-1

あなたはdelete.phpにアクセスして、誰もが適切な権限を持っていることを確認するために、同じ$_SESSIONロジックを使用することができます。

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_関数を使用すべきではないので、準備済みのステートメントを使用する必要があります。

+0

申し訳ありませんが、これは動作していないようです。あなたの答えをありがとう! @kchason – erikos93

0

isset関数の使用はここで解決します。 isset関数は、ユーザーが削除/変更リンクをクリックしたかどうかをチェックします(リンク先に直接delete.phpを貼り付けたもの)。したがって、ユーザーがリンクをクリックしたときにのみコードが実行されます。

if (isset($_GET['del'])) 
{ 
// your profile.php code here 
} 
else 
{ 
// error message 
} 
関連する問題