PHP password_hash, password_verify With Example

Introduction

PHP password_hash is a built-in function that is majorly used to encrypt the password string.

Encryption has c combination of random letters, numbers, and special characters.

PHP password_hash() function can create a unique password hash using a strong hashing algorithm which is only one-way hashing or encryption.

One-way hashing means, that generated password can not be decrypted.

PHP password_hash, password_verify With Example

In this article, we learn, to complete the PHP password hash concept and use the password verify function to confirm whether the password is correct or not with the help of an example.

Also Read: Complete Guide About Create PHP With MySQL Connection

Syntax:

The password hash function takes a maximum of 3 parameters.

  1. $string parameter takes the user’s password string which is used to hash and store on DB.
  2. $algo this parameter supports integer values that refer to some built-in PHP password hashing algos,
    • PASSWORD_DEFAULT: This is the default option and is recommended also. Because of developers when they want to add new algo options.
    • PASSWORD_BCRYPT: This algo uses the CRYPT_BLOWFISH algorithm to generate the hash.
    • PASSWORD_ARGON2I: Used Argon2 hashing algo.
  3. $options take some optional and advanced options like cost and salt.
    • Cost: for PASSWORD_BCRYPT it is the maximum algorithmic cost to be applied. 10 is the default. And it also affects the speed and loading time of the script.
    • Cost: For PASSWORD_ARGON2I maximum memory cost is to be applied to generate any hash.
    • Salt can be provided manually but is not recommended.

Also Read: How to Fetch Data From a Database in PHP Faster?

Example of PHP Password Hash

Output:

Result of crypt() on myPassword is $PjPYbvqoH26U

PHP password_hash and password_verify Example

Output:

$2y$10$dh8ntY.BhgCarjZuEwG70.vFLt4Af6vz08Ibd9TsMRv/4PegCJToS
Password verified

PHP password_verify Function

PHP password_verify Function is used to check the hashed password and verify whether it is matching with the original password or not.

Syntax:

Password verification takes only 2 parameters, one is input password by a user, and hashed password from DB.

Please let me know if you have to face any issues at the time of implementation.

To know more about PHP encryption algorithms, check here PHP: Password Hashing Functions – Manual.

Happy Coding..!

Was this article helpful?
YesNo

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

One thought on “PHP password_hash, password_verify With Example”

Leave a Reply

Your email address will not be published. Required fields are marked *