I am setting up a login for a website (perhaps the way I am doing it is just wrong, but that's for another question).
The way I am doing it is like this:
- Get username/password from user on login page.
- Pass these to my php using post.
- In the php, collect the values passed from post.
- Compare the password passed to PHP from post, with one taken from a MySQL database.
I can get the database to connect. I can extract the password from the database. I can collect the value from post. But I CANNOT compare these two values - it always says they are not equal, even though echoing them says they are.
To collect the posted values I use:
$posted_username = $_POST['uname'];
$posted_password = $_POST['passwd'];
To query the DB I use:
$result = mysql_query("SELECT passwd FROM usrs where usrname = '".$posted_username."'");
Next, if I include this line:
echo "Posted Username: " . $posted_username . "<br>" . " Posted Password: " . $posted_password . "<br>";
I can see that my posted values have been assigned correctly. I get this output when I run the PHP:
Posted Username: admin Posted Password: admin2
To step through my DB I use:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
And inside this while loop I go:
$password_fromdb = $row['passwd'];
echo "Password from db is: " . $password_fromdb . "<br>";
echo "Password from POST is: " . $posted_password . "<br>";
$equal = strcmp($password_fromdb,$posted_password);
The output of the two echo lines gives me this:
Password from db is: admin2 Password from POST is: admin2
So I can see that the two passwords DO match.
However, whether I use strcmp, or whether I use =, == or ====, I ALWAYS get told that my two passwords do not match. I get told they don't match even if they do!
WHAT am I doing wrong? I would appreciate help as I know nothing about PHP, have to do this for a class, and our tutuor has decided he's not going to TEACH us PHP.


strcmpreturns 0, if the two string are equal.=and====these would be incorrect for the following reasons. The first would be an assignment and so would be treated as true (if the assigned value was true) and the latter would be a parse error (unless it was a typo for===). Triple equals would return true only if the contents match and both values are of the same type i.e. string, which should be the case (and work) for your password comparisons.