How to Hash Password using PHP's password_hash()

Getting Started

I've used Bootstrap in this tutorial which is a CSS framework and is included in the downloadable of this tutorial but if you want, you may download Bootstrap using this link.

Creating our Database

First, we are going to create a database where we put our registered users/members. I've included a .sql file in the downloadable of this tutorial which is a mysql database file. All you have to do is import the said file. If you have no idea on how to do this, please refer to my tutorial, How import .sql file to restore MySQL database. You should be able to create a database with tables named test.

Creating our Register Form

Next, before we can login, we first need to register the user/member and for that we need to create a register form. Create a new file, name it as new_account.php and paste the codes below.
  1. <?php
  2. ?>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="utf-8">
  7. <title>How to Hash Password using PHP's password_hash()</title>
  8. <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  9. </head>
  10. <body>
  11. <div class="container">
  12. <h1 class="page-header text-center">Hash Password using PHP</h1>
  13. <div class="row">
  14. <div class="col-sm-4 col-sm-offset-4 panel panel-default" style="padding:20px;">
  15. <form method="POST" action="register.php">
  16. <p class="text-center" style="font-size:25px;"><b>Register</b></p>
  17. <hr>
  18. <div class="form-group">
  19. <label for="email">Email:</label>
  20. <input type="email" name="email" id="email" class="form-control" value="<?php echo (!empty($_SESSION['email']) ? $_SESSION['email'] : '') ?>">
  21. </div>
  22. <div class="form-group">
  23. <label for="password">Password:</label>
  24. <input type="password" name="password" id="password" class="form-control" value="<?php echo (!empty($_SESSION['password']) ? $_SESSION['password'] : '') ?>">
  25. </div>
  26. <div class="form-group">
  27. <label for="confirm_password">Confirm Password:</label>
  28. <input type="password" name="confirm_password" id="confirm_password" class="form-control" value="<?php echo (!empty($_SESSION['confirm_password']) ? $_SESSION['confirm_password'] : '') ?>">
  29. </div>
  30. <button type="submit" name="register" class="btn btn-success"><span class="glyphicon glyphicon-check"></span> Register</button> <a href="index.php">Already a member? Login</a>
  31. </form>
  32. <?php
  33. if(isset($_SESSION['error'])){
  34. ?>
  35. <div class="alert alert-danger text-center" style="margin-top:20px;">
  36. <?php echo $_SESSION['error']; ?>
  37. </div>
  38. <?php
  39.  
  40. unset($_SESSION['error']);
  41. }
  42.  
  43. ?>
  44.  
  45. </div>
  46. </div>
  47. </div>
  48. </body>
  49. </html>

Creating our Register Script

Next, we create the script to register our user. In here, we hash the password of the verified user to be inputted into our database. Create a new file, name it as register.php and paste the codes below.
  1. <?php
  2.  
  3. if(isset($_POST['register'])){
  4. //set database connection
  5. $conn = new mysqli('localhost', 'root', '', 'test');
  6.  
  7. //check if password matches the confirm password
  8. if($_POST['password'] == $_POST['confirm_password']){
  9. //check if email exists in the database
  10. $sql = "SELECT * FROM members WHERE email = '".$_POST['email']."'";
  11. $query = $conn->query($sql);
  12. if($query->num_rows > 0){
  13. $_SESSION['error'] = 'Email already taken';
  14. //return the inputted fields
  15. $_SESSION['email'] = $_POST['email'];
  16. header('location: new_account.php');
  17. }
  18. else{
  19. //hash the password
  20. $hash_password = password_hash($_POST['password'], PASSWORD_DEFAULT);
  21. $sql = "INSERT INTO members (email, password) VALUES ('".$_POST['email']."', '$hash_password')";
  22. if($conn->query($sql)){
  23. //unset user inputs
  24. unset($_SESSION['email']);
  25. unset($_SESSION['password']);
  26. unset($_SESSION['confirm_password']);
  27. $_SESSION['success'] = 'Member registered successfully';
  28. header('location: index.php');
  29. }
  30. else{
  31. $_SESSION['error'] = 'Something went wrong in adding member to database';
  32. header('location: new_account.php');
  33. }
  34. }
  35. }
  36. else{
  37. $_SESSION['error'] = "Passwords did not match";
  38. //return the inputted fields
  39. $_SESSION['email'] = $_POST['email'];
  40. $_SESSION['password'] = $_POST['password'];
  41. $_SESSION['confirm_password'] = $_POST['confirm_password'];
  42. header('location: new_account.php');
  43. }
  44. }
  45. else{
  46. $_SESSION['error'] = "Fill up register form first";
  47. header('location: new_account.php');
  48. }
  49.  
  50. ?>

Creating our Login Form

Next, we create our login form where we check our registered users/members. Create a new file, name it as index.php and paste the codes below.
  1. <?php
  2. ?>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="utf-8">
  7. <title>How to Hash Password using PHP's password_hash()</title>
  8. <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  9. </head>
  10. <body>
  11. <div class="container">
  12. <h1 class="page-header text-center">Hash Password using PHP</h1>
  13. <div class="row">
  14. <div class="col-sm-4 col-sm-offset-4 panel panel-default" style="padding:20px;">
  15. <form method="POST" action="login.php">
  16. <p class="text-center" style="font-size:25px;"><b>Login</b></p>
  17. <hr>
  18. <div class="form-group">
  19. <label for="email">Email:</label>
  20. <input type="email" name="email" id="email" class="form-control">
  21. </div>
  22. <div class="form-group">
  23. <label for="password">Password:</label>
  24. <input type="password" name="password" id="password" class="form-control">
  25. </div>
  26. <button type="submit" name="login" class="btn btn-primary"><span class="glyphicon glyphicon-log-in"></span> Login</button> <a href="new_account.php">Register a new account</a>
  27. </form>
  28. <?php
  29. if(isset($_SESSION['error'])){
  30. ?>
  31. <div class="alert alert-danger text-center" style="margin-top:20px;">
  32. <?php echo $_SESSION['error']; ?>
  33. </div>
  34. <?php
  35.  
  36. unset($_SESSION['error']);
  37. }
  38.  
  39.  
  40.  
  41. if(isset($_SESSION['success'])){
  42. ?>
  43. <div class="alert alert-success text-center" style="margin-top:20px;">
  44. <?php echo $_SESSION['success']; ?>
  45. </div>
  46. <?php
  47.  
  48. unset($_SESSION['success']);
  49. }
  50. ?>
  51. </div>
  52. </div>
  53. </div>
  54. </body>
  55. </html>

Creating our Login Script

Lastly, we create our login script to check if the user exists in our database. Create a new file, name it as login.php and paste the codes below.
  1. <?php
  2.  
  3. if(isset($_POST['login'])){
  4. //connection
  5. $conn = new mysqli('localhost', 'root', '', 'test');
  6.  
  7. //get the user with the email
  8. $sql = "SELECT * FROM members WHERE email = '".$_POST['email']."'";
  9. $query = $conn->query($sql);
  10. if($query->num_rows > 0){
  11. $row = $query->fetch_assoc();
  12. //verify password
  13. if(password_verify($_POST['password'], $row['password'])){
  14. //action after a successful login
  15. //for now just message a successful login
  16. $_SESSION['success'] = 'Login successful';
  17. }
  18. else{
  19. $_SESSION['error'] = 'Password incorrect';
  20. }
  21. }
  22. else{
  23. $_SESSION['error'] = 'No account with that email';
  24. }
  25. }
  26. else{
  27. $_SESSION['error'] = 'Fill up login form first';
  28. }
  29.  
  30. header('location: index.php');
  31.  
  32. ?>
That ends this tutorial. Happy Coding :)

Add new comment