Advertise your products on NaijaTech

php script

The Best and sustainable alternative to MMM is here; GetHelpWorldwide. Click here for details

Hit big money online! Over N12,000 flowing to your bank account daily. Click here

To protect password from users of your website or application from intruders, you may need to secure them by hashing or not storing the password in plain text format on the database.

Popular open-source like wordpress uses this technique to secure password of users.

This technique involves collecting passwords from your apps, hashing it before storing on the database. This ensures that when intruders get access to your database, they will not still be able to login with the username and password of the user on the database as password will be a long string of text that is not the original password that the user used while creating the account.

In this tutorial, I will be showing php script that you can easily use to achieve this.

For securing password during account creation for example:

public function hash_it($username, $password)
 $salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
 $salt = base64_encode($salt);
 $salt = str_replace('+', '.', $salt);
 $hashedpassword = crypt($password, '$2y$10$' . $salt . $username);
 return $hashedpassword;

//$salt is random string generated for the hashing
//$username is the username obtained from the user
//$password is the plain text password obtained from the user.

For login or authentication:

public function checkIfCorrect($password)
//get the hash attached to the username of the person trying to login. $gottenhash
$gottenHash = "";//this line is for example purpose,adjust according to your own execution.

if(crypt($password, $gottenHash) == $gottenHash)
 return true;
 return false;
//$gottenHash is the hashed password stored on the database for example;
//$password is the plain text password received from the user.

For how any question, clarification or how to integrate in your app, you can drop comments in the comment section.

This is NaijaTech

Share This: