Forum Moderators: coopster

Message Too Old, No Replies

Tiny variable issue

         

Orangutang

1:31 pm on Jul 15, 2010 (gmt 0)

10+ Year Member



Hi,

If anyone can look at the code below and identify why my variables do not work properly it would be much appreciated. All I'm trying to do is create a couple of variables so I can check them against each other

Bold text indicates problem:

If I run this code it works and I login:

if ($numrows != 0) // If numrows is either or equal to 0 - IE No data
{
while ($row = mysql_fetch_assoc($query)) // Fetches actual username and password
{
// $username = $row['$username'];
// $password = $row['$password'];
}
If ($username==$username && $password==$password) // Checks to see if matches
{
echo "You are logged in. <a href='clientscontrolpanel.php'>Click here to go to the Control Panel.</a>";
}
else
echo "Incorrect password";
}
else
echo "Username or Password not found!";

But if I run this I am not - Incorrect password echoed.

if ($numrows != 0) // If numrows is either or equal to 0 - IE No data
{
while ($row = mysql_fetch_assoc($query)) // Fetches actual username and password
{
$dbusername = $row['$username'];
$dbpassword = $row['$password'];
}
If ($username==$dbusername && $password==$dbpassword) // Checks to see if matches
{
echo "You are logged in. <a href='clientscontrolpanel.php'>Click here to go to the Control Panel.</a>";
}
else
echo "Incorrect password";
}
else
echo "Username or Password not found!";

Many thanks if you can shed some light on it. I'm guessing it is something to do with xampp configuration because this error seems so silly I can't be something like that can it ?

Wayder

2:06 pm on Jul 15, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



$dbusername = $row['$username'];
$dbpassword = $row['$password'];

should be

$dbusername = $row['username'];
$dbpassword = $row['password'];

No $

Orangutang

2:54 pm on Jul 15, 2010 (gmt 0)

10+ Year Member



Thanks Wayder, syntax, syntax, syntax....

I also noticed that the input in the form is caps sensitive. When iuput Orang incorrect pw but orang logs in.

andrewsmd

3:09 pm on Jul 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All php is case sensitive. I.e. the variable $var is not the same as $Var. If you want to not check on case, then use the to lower or to upper methods to put everything into either all lower or upper case.

rocknbil

4:39 pm on Jul 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When iuput Orang incorrect pw but orang logs in.


He/she is talking about input, not variables. When you use the equality operator,

if (($username==$row['username']) and .....

it has to be an exact match, which is what you want with a password and *possibly* user name as well. If you want case insensitivity on the user name, use preg_match with the i modifier:

$username=str_replace(' ','',$username); strip spaces, or use preg
if (preg_match("/^$username$/i",$row['username']) and ....

^ and $ mean starts and end of the string respectively so that something like "anorange" or even part of it, "oran," won't match if it's supposed to be "orang." The i modifier makes it case insensitive and will match on Orang, oRanG, ORANG . . . .