0

投稿の表示を許可されているユーザーをスキャンするこのスクリプトを以下に示します。閲覧者のIDがフィールドに保存されているIDと一致するように更新するにはどうすればよいですか。一致する場合は機能し、そうでない場合は機能しません。保存されるエントリは、99394david、324234smith、34343janeのようになります。したがって、私が持っているこのスクリプトはそれと一致していません。

    $kit = mysql_real_escape_string($_GET['id']);

$sql="SELECT `Who_can_see` from `posts` where `post_id` = '$kit'";  
    $result=mysql_query($sql);

    $query = mysql_query($sql) or die ("Error: ".mysql_error());

    if ($result == "")
    {
    echo "";
    }
    echo "";


    $rows = mysql_num_rows($result);

    if($rows == 0)
    {
    print("");

    }
    elseif($rows > 0)
    {
    while($row = mysql_fetch_array($query))
    {
    $userallowed = htmlspecialchars($row['who_can_see']);
    }
    }

    //$personid is drawn from the database.  its the id of the
     person viewing the link.

    if ( $userallowed == $personid ) {
echo("allowed");
    } else {
echo("not allowed");
    die();
    }

    ?>
4

1 に答える 1

0

クエリに を追加するだけです$personid(ただし、テーブルを正確に埋める方法については疑問がありますposts...)。

$sql="SELECT `Who_can_see` from `posts`
      where `post_id` = '$kit'
        AND `Who_can_see` = '$personid'";

結果に行が含まれている場合、ユーザーは投稿を表示できます。

ちなみに、潜在的なSQLインジェクションの問題を回避するために、準備済みステートメントを使用することもお勧めします。

編集:Who_can_seeエントリのコンマ区切りリストを含めることができるという事実に基づいて、元のスクリプトを使用し、たとえばstripos.

if ( stripos($userallowed, $personid) !== false ) {
    // $personid is found in $userallowed
    echo("allowed");
} else {
    echo("not allowed");
    die();
}
于 2012-04-26T01:01:22.687 に答える