login script not working...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
max0005
Forum Newbie
Posts: 2
Joined: Wed May 06, 2009 10:51 am

login script not working...

Post 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?
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Re: login script not working...

Post 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).
Post Reply