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 curl
Mar 22, 2023 programming

PHP curl

Today we are going to talk about curl in PHP. Sometimes we need to send requests to another url to get data, maybe another API. So how can we do that in PHP? curl is the answer. ...

6 Min Read Read More
PHP Interacting with Python
Mar 22, 2023 programming

PHP Interacting with Python

Today we are going to talk about interacting with a python file in PHP. Both PHP and Python are very powerful languages. you can run a python program from your PHP code. ...

6 Min Read Read More
A Beginner's Guide to Different Types of Software Testing
Apr 07, 2024 programming

A Beginner's Guide to Different Types of Software Testing

Software testing is a crucial aspect of programming and software development. By testing, developers can ensure that their software performs as expected. ...

9 Min Read Read More
AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers
Jul 18, 2025 programming

AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers

In today’s web development landscape, AngularJS may no longer dominate headlines, but it still powers countless legacy applications. Many organizations—especially in finance, healthcare, and enterprise software—rely on AngularJS systems that require ongoing maintenance and enhancements. ...

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