Page 1 of 1

login script not working...

Posted: Thu May 07, 2009 11:14 am
by max0005
Hello,
I'm working on a login script in php; this is the script:

Code: Select all

<html>
<head>
</head>
<body>
<?php
$uname=$_POST["username"];
$tpass=sha1($_POST["password"]);
 
 
$connection = mysql_connect ("localhost", "root");
if (!$connection)
{
die ('Could not connect: ' . mysql_error());
}
 
mysql_select_db("cms", $connection);
 
$select = "SELECT * FROM users
    WHERE name='$uname' and password='$tpass')";
 
mysql_query($select);
 
if($select = 0)
{
    print "Not logged in :(";
}
else
{
    print "Logged in!";
}
 
?>
</body>
</html>
The value $uname is the username inserted from the user when (s)he attempts to login (inserted in another page) while name is the column in the "users" table where the value added upon registration is stored.

$tpass is the encrypted version of the password entered upon login which has to be confronted with the one stored in database. What I want it to do is to search in the database until a record in which name=$uname and password=$tpass and if that is true print "Logged in" otherwise print "not logged in" however it always prints logged in... any ideas?

Re: login script not working...

Posted: Thu May 07, 2009 10:37 pm
by mickd
$select is just a string that holds the query you're going to give to mysql_query. So you can't just do:

Code: Select all

 
if($select = 0) {
// not logged in
} else {
// logged in
}
 
What the above is actually doing is giving $select the value of 0, which gets assigned in the if. You can't use == to compare the value too, because $select is just a string.

This will solve your problems :)
mysql_num_rows
Have a look at the example from the manual, and basically you'll just need to run an if to check if $num_rows is 0 (no record exists), or if $num_rows is 1 (a record exists).