First, a suggestion: do not specify if the password is wrong, or login is wrong. This allows hackers and such to know that that user exists on your system. I pefer saying "Invalid username/password combination".
here is a very simple script, although it could be expanded on and optimized to the point that if they've entered too short a username or password, it will give them the error without needing to perform the query (thus saving DB load)...
<?php
$sock = mysql_connect("host",user,pass);
$message = "Enter your username & password";
$error_message = "Invalid username/password combination";
if ($login) {
if ($username=="" || $password=="")
$message = $error_message;
else {
$query = "SELECT password FROM users WHERE username='$username'";
$res = mysql_db_query("database",$query,$sock);
$num = mysql_numrows($res);
if ($num==0)
$message = $error_message;
else {
$row = mysql_fetch_row($res);
if ($row[0]=='$password') {
$message = "Login successful.";
$success=1;
}
else
$message = $error_message;
}
}
mysql_close($sock);
}
if ($success)
echo $message;
else {
echo $message;
//echo your form here....
}
and that should be it...
you'll notice instead of selecting where username='' AND password='' in my query, which would have saved some of the coding, i chose to select out the password and compare it in php instead. this is because comparing in the db versus comparing in php, php is a lot faster. for this case the difference might not be noticeable, but in future applications it might be =)
--
Keith Bussey
kbussey@wisol.com
Programmer - WISOL.com
(514) 398-9994 ext. 225