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, complete the PHP password hash concept and use the password verify function to confirm 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 value that refer to some built-in PHP password hashing algos,
    • PASSWORD_DEFAULT: This is the default option and recommended also. Because of developers when they want to add new algo options.
    • PASSWORD_BCRYPT: This algo uses CRYPT_BLOWFISH algorithm to generate hash.
    • PASSWORD_ARGON2I: Used Argon2 hashing algo.
  3. $options takes some optional and advance options like, cost and salt.
    • Cost: for PASSWORD_BCRYPT it is maximum algorthimic 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 to be applied to generate any hash.
    • Salt can be provide manually but 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 it is matching with an original password or not.

Syntax:

Password verify 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
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.
Posts created 188

One thought on “PHP password_hash, password_verify With Example

Leave a Reply

Your email address will not be published.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top