Page 1 of 1

Using md5(); For Passwords

Posted: Thu Feb 06, 2003 2:47 pm
by icesolid
Can anyone show me some sample code on how to use md5(); for passwords in a database. I would also like to see some sample code on how to check the md5(); for passwords in a database. The database I am using is MySQL.

So basically a log in script using md5();

I use a log in script now that I made, but I do not use md5(); and I really would like to for user passwords.

Thanks.

Posted: Thu Feb 06, 2003 4:29 pm
by bionicdonkey
$password = md5($password);

that is the main bit u will need to know.
before sending the password to the db do that.
for checking the password, do that to the one the person entered then check to see if it's the same as the one in the db

Posted: Thu Feb 06, 2003 4:31 pm
by daven
Insert new user into db

Code: Select all

<?php
$password=md5("User Password");
$username="username";

$qry="INSERT INTO userTable(username,password) VALUES('".$username."', '".$password."')";
mysql_query($qry,$conn) or die("Unable to insert");
?>
After username/password are entered, check to see if they exist

Code: Select all

<?php
$username=$_POST[username];
$password=md5($_POST[password]);
if($username!="" && $password!=""){ // just to make sure they are not blank
  $qry_login="SELECT * FROM userTable WHERE username='".$username."' AND password='".$password."'";
  $result=mysql_query($qry_login, $conn) or die("username fail");
}
?>
Note: MD5 creates a hash, not an encryption. You cannot decode a hash, you can only compare it. However, a given string will give the same hash every time (md5("hello") == md5("hello") != md5("goodbye")).