Free cookie consent management tool by TermsFeed Generator PHP Password Hashing | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Password Hashing

PHP Password Hashing

Last Updated on Mar 22, 2023

Introduction

Do you remember how facebook stored millions of user’s passwords in plain text?

Don’t be like facebook!

Storing and managing passwords is very important. And in PHP it’s very easy.

Password Hash

The first step is hashing the password before storing it in database and we can do  that with Password_hash

Password_hash(password,algorithm,options)

It takes 3 arguments

  • Password
  • Flag for algorithm
  • Extra options

So let’s use it.

$password = 1234569;
$hash = password_hash($password, PASSWORD_DEFAULT);
echo $hash;
// $2y$10$bXwHuVwJsxrcZ6aKh9mAFO0mW0LySI2caHySsYMeWMfi0g3Y8LUoW

It’s a one way hash. You cannot change the hashed password back to the original password.

Password Verify

Then how can we verify if the user is typing the correct password?

Again, very easy. We can do that with password_verify

password_verify(password,hash)

It takes 2 arguments

  • Password that the user is typing 
  • Hash is the hash of the original password that we saved

Returns true if matched and false if didn’t match

$password = 1234569;
$hash = password_hash($password, PASSWORD_DEFAULT);
if(password_verify($password,$hash)){
   echo 'password is correct';
}else{
   echo 'password is wrong';
}
// password is correct

And for wrong password:

$password = 1234569;
$hash = password_hash($password, PASSWORD_DEFAULT);
if(password_verify(9266984161,$hash)){
   echo 'password is correct';
}else{
   echo 'password is wrong';
}
// password is wrong

See how easy it is to hash the passwords? Never store any password without hashing them first.

https://youtu.be/bcqVgQ9M1as

Conclusion

Now you know about hashing and verifying passwords in PHP.

I recommend you to open a PHP files and try to hash and verify passwords. security of your application is very important.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • password hash in PHP
  • password verify in PHP
  • security

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP range
Feb 15, 2023 programming

PHP range

Today we are going to talk about range function in PHP. It's a very simple yet very powerful tool and can save you a lot of time. it's also more readable than the alternatives. ...

3 Min Read Read More
PHP array change key case
Feb 14, 2023 programming

PHP array change key case

Today we are going to talk about changing the case of the keys of an array in PHP. Sometimes you might change the case of the keys in your array. It's very easy to do that in PHP. ...

4 Min Read Read More
3 Ways to add Conditional and Loop within HTML
Mar 21, 2023 programming

3 Ways to add Conditional and Loop within HTML

Today we are going to talk about different ways we can add PHP conditionals and loops within HTML . ...

9 Min Read Read More
Creating a Simple Template Engine like Laravel Blade in PHP
Aug 21, 2023 programming

Creating a Simple Template Engine like Laravel Blade in PHP

Today we are going to learn how to build a simple template engine like Laravel's Blade. it's a great learning opportunity to understand how popular engines like Blade might work under the hood. ...

4 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...