i made a registration form and an login form.
i used first used trim()
and then md5()
to store the password in database.
i used the same to check the password in database in the login form, but for some unknown reason i cant match the md5 password from the login form.
All i want is to retrieve the password from the database.
The problem is when i store it in database and check it in the php myadmin the md5 password is something like 123456789
and when i echo'd it in login form it gives me this 1234567890
. a zero in the end tht zero is creating the problem otherwise the whole md5 password is matched. my database collation is latin_1_swedish_ci
.
is there any collation problem ?
i hv used varchar value 255 then text etc. but no solution.
is there any problem in the format or structure ?
please help me retrieve the md5 password correctly eliminating the unwanted zero .
function login()
{
if(isset($_POST['login']))
{
if(!$_POST['username'] || !$_POST['password'])
{die (mysql_error());}
else
{
$username = trim($_POST['username']);
$username = addslashes($_POST['username']);
$password = trim($_POST['password']);
$password = md5($_POST['password']);
echo $password;
$check_username = mysql_query("SELECT * FROM users WHERE username = '".$username."'")or die(mysql_error());
$check_username2 = mysql_num_rows($check_username);
if ($check_username2 == 0)
{die ("Username and password doesnt exist");}
else
{
$check_pass = mysql_query("SELECT * FROM users WHERE username = '".$password."'")or die(mysql_error());
$check_pass2 = mysql_num_rows($check_pass);
echo $check_pass2;
}
}
}
}
and
function register()
{
if(isset($_POST['submit']))
{
if(!$_POST['first_name'] || !$_POST['last_name'] || !$_POST['username'] || !$_POST['password'])
{header ("Location:user_registration.php");}
else
{
$firstname = trim($_POST['first_name']);
$firstname = addslashes($_POST['first_name']);
$lastname = trim($_POST['last_name']);
$lastname = addslashes($_POST['last_name']);
$username = trim($_POST['username']);
$username = addslashes($_POST['username']);
$password = trim($_POST['password']);
$password = md5($_POST['password']);
connect();
if($mysql_check_user = mysql_query("SELECT * FROM `users` WHERE `username` LIKE '$username' "))
{
$check_row = mysql_num_rows($mysql_check_user);
if($check_row > 0)
die ("username Exist");
else
{
$store = mysql_query
("INSERT INTO `project_upload`.`users`
(`id`, `fname`, `lname`, `username`, `password`)
VALUES (NULL, '$firstname', '$lastname', '$username', '$password')");
}
if ($store)
{echo "You have regstered succesfully. please go to <a href = 'uploads_login.php'>login page</a>"; }
else {echo mysql_error();}
}
}
}
}
trim()
on a password?trim()
result actually gets thrown away, so this isn't the problem. The secondSELECT
statement inlogin()
is searching for a user whose name ismd5($_POST['password'])
, which seems wrong to me. Also, MD5 never returns a 9 or 10-digit string. In short - it's impossible to see what's going on here...