View Single Post
Old 03-18-2009, 05:22 PM   #7 (permalink)
CrazeD
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,691

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: PHP form password with MD5

You do it the same way, and then just compare.

PHP Code:
   <?php

/*
make the createSalt function which will make a randomly generated string for the password salt

a password salt is a short randomly generated string that will make a password harder to crack, increasing security
*/

function createSalt($length='')
{
    
// make the randomly generated string, with a few standard functions
    
$salt substr(sha1(md5(uniqid(rand(), true))), 0$length);

    return 
$salt;
}

/*
make the getPassHash function which will use a custom algorithm to encrypt the password and salt
*/

function getPassHash($password='',$salt='')
{
    
// make the hash string using sha1 and md5, and add the salt in
    
$passhash sha1 (md5(sha1($password) . md5($salt)));

    return 
$passhash;
}

$con mysql_connect("localhost","user","password1234");

if(!
$con)
    {
    die(
'Could not connect: ' mysql_error());
    }

// get POST data and use mysql_escape_string to escape illegal characters
// this is to prevent SQL injection, which is a huge but easy-to-fix security risk
$username mysql_escape_string ($_POST['username']);
$password mysql_escape_string ($_POST['password']);

// check if username or password is empty
if (!empty ($username) && !empty ($password)) {        
    
mysql_select_db("database"$con);

    
// do a query to find the member with the username from the form
    
$sql mysql_query ("SELECT * FROM members WHERE username='".$username."' LIMIT 1");

    
// check if rows were found (indicating the user exists)
    
if (mysql_num_rows ($sql) > 0) {
        
// assign $row to the database rows
        
$row mysql_fetch_array ($sql);

        
// get the salt from the database
        
$salt $row['password_salt'];

        
// make the password hash with the form password and the salt returned from the database
        
$passhash getPassHash($password,$salt);

        if (
$passhash == $row['password']) {
            
// member is logged in
            // set some sessions, or whatever else you want to do
            
echo 'You are now logged in!';
        } else {
            
// invalid password
            
echo 'Your password is incorrect!';
        }
    } else {
        
// username wasnt found
        
echo 'This user is not in the database!';
    }
} else {
    
// username or password not entered
    
echo 'You must enter a username and a password!';
}

mysql_close($con);

?>

__________________

Need website help? PM me!
CrazeD is offline