How to use PHP's password_hash to hash and verify passwords

Josh Potter

Recently I have been trying to implement my own security on a log in script I stumbled upon on the internet. After struggling of trying to learn how to make my own script to generate a salt for each user, I stumbled upon password_hash.

From what I understand (based off of the reading on this page), salt is already generated in the row when you use password_hash. Is this true?

Another question I had was, wouldn't it be smart to have 2 salts? One directly in the file and one in the DB? That way, if someone compromises your salt in the DB, you still have the one directly in the file? I read on here that storing salts is never a smart idea, but it always confused me what people meant by that.

Akar

Using password_hash is the recommended way to store passwords. Don't separate them to DB and files.

Let's say we have the following input:

$password = $_POST['password'];

You first hash the password by doing this:

$hashed_password = password_hash($password, PASSWORD_DEFAULT);

Then see the output:

var_dump($hashed_password);

As you can see it's hashed. (I assume you did those steps).

Now you store this hashed password in your database, ensuring your password column is large enough to hold the hashed value (at least 60 characters or longer). When a user asks to log them in, you check the password input with this hash value in the database, by doing this:

// Query the database for username and password
// ...

if(password_verify($password, $hashed_password)) {
    // If the password inputs matched the hashed password in the database
    // Do something, you know... log them in.
} 

// Else, Redirect them back to the login page.

Official Reference

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use php's password_hash() method..?

From Dev

Security on hashing passwords with CryptoJS and then use php password_hash()

From Dev

Are PHP's password_hash and password_verify functions enough?

From Dev

PHP password_hash(), password_verify()

From Dev

php password_hash and password_verify issues no match

From Dev

PHP password_hash and password_verify Not Working with MySQL

From Dev

php password_hash and password_verify issues no match

From Dev

How is the randomly generated password salt in PHP 5.5's new password_hash function even useful?

From Dev

How is the randomly generated password salt in PHP 5.5's new password_hash function even useful?

From Dev

Wanting to hash passwords using password_hash but still want to be able to display them with an eye like lastpass php

From Dev

compare passwords on register using password_hash()

From Dev

Using password_hash and password_verify

From Java

PHP can not login with correct password using password_hash()/password_verify()

From Dev

Why isn't PHP's "password_hash" returning a string?

From Dev

correct use of password_hash

From Dev

correct use of password_hash

From Dev

PHP Password_Hash Function

From Dev

php password_hash and password_verify looked all over still doesn't work

From Dev

Is PHP password_hash() + password_verify() safe today (May 2016)?

From Dev

php password_hash and password_verify looked all over still doesn't work

From Dev

Using password_hash and password_verify from different objects in OO PHP

From Dev

How to get the random salt from password_hash in PHP?

From Dev

php password hash and verify !false

From Dev

password_verify can't verify password_hash with Bcrypt

From Dev

password_hash and password_verify returns false

From Dev

password_hash equivalent for php 5.4?

From Dev

PHP password_hash Check Two Hashes

From Dev

Best alternative for password_hash in PHP 5.3.27?

From Dev

Implementing PHP password_hash() in MSSQL

Related Related

  1. 1

    How to use php's password_hash() method..?

  2. 2

    Security on hashing passwords with CryptoJS and then use php password_hash()

  3. 3

    Are PHP's password_hash and password_verify functions enough?

  4. 4

    PHP password_hash(), password_verify()

  5. 5

    php password_hash and password_verify issues no match

  6. 6

    PHP password_hash and password_verify Not Working with MySQL

  7. 7

    php password_hash and password_verify issues no match

  8. 8

    How is the randomly generated password salt in PHP 5.5's new password_hash function even useful?

  9. 9

    How is the randomly generated password salt in PHP 5.5's new password_hash function even useful?

  10. 10

    Wanting to hash passwords using password_hash but still want to be able to display them with an eye like lastpass php

  11. 11

    compare passwords on register using password_hash()

  12. 12

    Using password_hash and password_verify

  13. 13

    PHP can not login with correct password using password_hash()/password_verify()

  14. 14

    Why isn't PHP's "password_hash" returning a string?

  15. 15

    correct use of password_hash

  16. 16

    correct use of password_hash

  17. 17

    PHP Password_Hash Function

  18. 18

    php password_hash and password_verify looked all over still doesn't work

  19. 19

    Is PHP password_hash() + password_verify() safe today (May 2016)?

  20. 20

    php password_hash and password_verify looked all over still doesn't work

  21. 21

    Using password_hash and password_verify from different objects in OO PHP

  22. 22

    How to get the random salt from password_hash in PHP?

  23. 23

    php password hash and verify !false

  24. 24

    password_verify can't verify password_hash with Bcrypt

  25. 25

    password_hash and password_verify returns false

  26. 26

    password_hash equivalent for php 5.4?

  27. 27

    PHP password_hash Check Two Hashes

  28. 28

    Best alternative for password_hash in PHP 5.3.27?

  29. 29

    Implementing PHP password_hash() in MSSQL

HotTag

Archive