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 Working with Forms
Mar 21, 2023 programming

PHP Working with Forms

Today we are going to talk about working with forms in PHP. It's very important to know how to work with forms and parameters in the request, whether they are from a Get request or a Post request. ...

9 Min Read Read More
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 Testing with PHPUnit and Pest
Mar 22, 2023 programming

PHP Testing with PHPUnit and Pest

Today we are going to write tests with PHPUnit and Pest in PHP. Testing is a very important step in developing an application. phpunit and pest make it really easy to write tests in php ...

12 Min Read Read More
PHP Callback Functions
Jan 08, 2023 programming

PHP Callback Functions

Today we are going to talk about callback functions in PHP. sometimes we need to pass a function as an argument of another function, that's exactly what callback function is. ...

7 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 ...