0

I am trying to check if a user is the owner of a profile page so that i can display a text box for entering twitter similar posts.

The code

$id=mysql_query("SELECT id FROM users WHERE`username`='".$_GET['username']."'");

$ultimatum_form ='';
if(isset($_SESSION['id'])){
    if($_SESSION['id']==$id){
        $ultimatum_form = 'Write an ultimatum!(220 char max)<br/>
        <form action="profile.php" method="post" enctype="multipart/form-data" name="ultimatum_form">
        <textarea name="ultimatum_field" rows="3" style="width:97%;"></textarea>
        </form>';
    }
}
print "$ultimatum_form";

in my DB i have a table called "users", the table users has the columns "firs", "last", "username", "password", "email" and "id".

If i set $ultimatum_form outside of the session check it outputs the text-field and it works. The problem is that if i then go to another persons profile i can see the text-field and write posts for them.

2 Answers2

1

You need to get data from the query:

$id=mysql_query("SELECT id FROM users WHERE`username`='".$_GET['username']."'");
$r = mysql_fetch_assoc($id);
$id = $r['id'];

then $id contains the row "id" value.
look into pdo/mysqli, mysql won't wrok in next php version.

here, better:

  $id=mysql_query("SELECT id FROM users WHERE`username`='".$_GET['username']."'");
    $r = mysql_fetch_assoc($id);
    $id = $r['id'];


    if(isset($_SESSION['id']))
    {
        if($_SESSION['id']==$id)
        {
            echo '
             Write an ultimatum!(220 char max)<br/>
            <form action="profile.php" method="post" enctype="multipart/form-data" name="ultimatum_form">
            <textarea name="ultimatum_field" rows="3" style="width:97%;"></textarea>
            </form>';
        }
        else
        {
            // Not users profile page
        }
    }
William N
  • 432
  • 4
  • 12
0

You can't exactly do what you're doing. You need to fetch the records.

$results = mysql_fetch_array(mysql_query("SELECT id FROM users WHERE`username`='".$_GET['username']."'"));

Then you can do:

if($_SESSION['id']==$results['id']){
SeanWM
  • 16,789
  • 7
  • 51
  • 83