Using md5(); For Passwords

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
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Using md5(); For Passwords

Post 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.
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post 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
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

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