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

Top 5 websites to learn PHP for FREE
Dec 22, 2022 programming

Top 5 websites to learn PHP for FREE

Do you want to learn PHP but you don’t want to spend hundreds of dollars? You are not alone. There are many websites that help people like you and me to start our journey. ...

9 Min Read Read More
PHP DataTypes
Mar 21, 2023 programming

PHP DataTypes

Today we are going to learn about Data Types in PHP. Data types are another important subject in programming. The better you know the data types you are working with, the more skillful you become. ...

5 Min Read Read More
PHP Database
Mar 22, 2023 programming

PHP Database

Today we are going to talk about working with a database in PHP. working with databases is a very important part of web applications. you should be able to store data and read them. ...

14 Min Read Read More
PHP Interface
Mar 22, 2023 programming

PHP Interface

Today we are going to talk about interfaces in PHP. Interface is like a skeleton for the class. Interface is an important topic in OOP and it helps you manage your code better. ...

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