To create a secure password in PHP, we need to hash the password. Hashing is a process of converting plaintext passwords into unintelligible strings that can't be reversed. We can use one of the built-in hashing functions in PHP such as password_hash()
.
main.php163 chars4 lines
In the above example, we have first created a plaintext password string. We then pass this password to the password_hash()
function along with the hashing algorithm to be used. In this case, we have used the PASSWORD_DEFAULT
algorithm, which currently is bcrypt. The function then returns the hashed password string which is then stored in the database.
When a user logs in and enters their password, we can then match their plain text password with the hashed version using password_verify()
function.
main.php234 chars9 lines
In the above example, we have a stored hash value from the database and a plain text password entered by the user during login. We then use the password_verify()
function to check if the entered password matches the stored hash. If they match, then the user is logged in successfully.
gistlibby LogSnag