Avatar ·

How to properly use bcrypt for password hashing in PHP?

Hello everyone. I'm a beginner PHP developer and I'm currently working on a user registration and authentication system. I've read that storing passwords in plain text is a very bad practice and that hashing should be used. I found information about bcrypt, but I don't fully understand how to use it correctly in PHP.

\n\n

I tried using password_hash() and password_verify(), but I'm not sure I'm doing everything right. Here's an example of my code:

\n\n
<?php\n// Registration\n$password = $_POST['password'];\n$hash = password_hash($password, PASSWORD_DEFAULT);\n// Saving $hash to the database...\n\n// Authentication\n$input_password = $_POST['password'];\n$stored_hash = // retrieving from the database\nif (password_verify($input_password, $stored_hash)) {\n    echo "Success!";