Zend - The PHP Company




Security

Add Code


Hashed & encrypted passwords and usernames login system/functions  

Type: code fragment
Added by: danzarrella
Entered: 30/01/2002
Last modified: 02/12/2001
Rating: **** (7 votes)
Views: 18107
Very simple functions to create a logins table in a mysql db, add hashed/encrypted usernames and passwords, and authenticate.


<?
function MakeTableLogins($database$host$db_user$db_pass) {//create the logins table
    
$linkID mysql_connect($host$db_user$db_pass);
    
mysql_select_db($database$linkID);
    
mysql_query("create table logins (user char(32), pasword char(32))"$linkID);
}

function 
Encrypt($string) {//hash then encrypt a string
    
$crypted crypt(md5($string), md5($string));
    return 
$crypted;
}

function 
AddUser($database$host$db_user$db_pass$username$password) { //add user to table logins
    
$linkID mysql_connect($host$db_user$db_pass);
    
mysql_select_db($database$linkID);
    
$password encrypt($password);
    
$username encrypt($username);
    
mysql_query("insert into logins values ('$username', '$password')"$linkID);
}

function 
Login($database$host$db_user$db_pass$user$password) { //attempt to login false if invalid true if correct
 
$auth false;
 
$user Encrypt($user);

 
$linkID mysql_connect($host$db_user$db_pass);
 
mysql_select_db("$database"$linkID);
 
$result mysql_query("select password from logins where user = '$user'"$linkID);
 
$pass mysql_fetch_row($result);
 
mysql_close($linkID);

 if (
$pass[0] === (Encrypt($password))) {
     
$auth true;
 }
 return 
$auth;
}
?>


Usage Example




Rate This Script





Search



This Category All Categories