login help

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
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

login help

Post by tommy1987 »

Hi im trying to write a script which lets users log in. It validates users against a database of users, I want to be able to take variables posted from a form, search for something like:

Code: Select all

$user = $_POST['login_username'];
$query = "select * from users where user = $user";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());

if($_POST['login_username'] == $row['user'] && $_POST['login_password'] == $row['pass'])
{
//login correct
echo "Thanks ".$row['usr']." for logging in";
$_SESSION['logged_in'] = "YES";

} elseif($_POST['login_password']) {
//login incorrect
echo 'Incorrect login '.$_POST['login_username'];
}
The way I anticipate it working is searching for the user in the table of the username they enter, then getting the password to mathc and checking it against the one they entered. It doesnt seem to work though, it says stuff about error in SQL syntax, I believe the problem is here.

Hope someone can help, Thanks in advance, Tom
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Post by someberry »

What's the problem?

I would personally check the database for the username and password simultaniously, and then check the number of returned rows - would be more efficient.
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

Could someone please write a code example to support what has been suggested, I am new to mySQL and would like to see the format of what he means.

Thanks, Tom
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Could you please search the web / this forum for an example.. It has been written many times before... So i'm sure you'll find a good example...

Eg: i found http://www.yeswebmaster.com/article.php?id=45 when i searched for 'php mysql login select count'. (Be aware that you should use http://www.php.net/mysql_real_escape_string instead of the addslashes in that article though)
Post Reply