Most of the websites are providing sing up and login facility to the user. User has to create a password and use it for login to the website. But it is very important to secure the password of the user. password_hash() function provides the facility to securely store the password of the user to the database.
SyntaxÂ
Â
password_hash(Password, PASSWORD_DEFAULT)
Example: First parameter Password will contain the normal password. The second Parameter will contain PASSWORD_BCRYPT to make secure otherwise it contains PASSWORD_DEFAULT as default. Let’s see the example to understand properly.Â
Â
- dbconn.phpÂ
Â
php
<?php   $db_host = "localhost";  $db_name = "secure_pass";  $db_pass = "";  $db_user = "root";Â
  $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);Â
  if (!$conn){    die ('Failed to connect with server');  }   ?> |
- Signup Form:Â
Â
html
<form action="index.php" method="POST">Â Â <label for="username">Username</label>Â Â <input type="text" name="username" required><br><br>Â
  <label for="password">Password</label>  <input type="password" name="password" required><br><br>  <input type="submit" name="submit" value="submit">   </form> |
- index.phpÂ
Â
php
<?php   //Include database connection file  include 'dbconn.php';Â
  if (isset($_POST['submit'])){    $username = $_POST['username'];Â
    // Normal Password    $pass = $_POST['password']; Â
    // Securing password using password_hash    $secure_pass = password_hash($pass, PASSWORD_BCRYPT);Â
    $sql = "INSERT INTO login_tb (u_username, u_password)    VALUES('$username', '$secure_pass')";    $result = mysqli_query($conn, $sql);  }    // Include HTML sign up form  include 'signup_form.php';?> |
- Output:Password In Database.Â
Â
Â

