How to Verify Password in PHP from Database

2 years ago admin PHP

In this tutorial we will see how to save the password in the database using PHP and MySQL, then we will see how to verify the hashed saved password with the password entered by the user.


Store the hashed password

To store the password we use the password_hash function which takes the password, the algorithm used for hashing, and the options.

                                                    
                                                                                                                
<?php

class UsersController{
    //store new user
    public function register(){
        $options = [
            "cost" => 12
        ];
        $password = password_hash($_POST["password"],PASSWORD_BCRYPT,$options);
        $data = array(
            "fullname" => $_POST["fullname"],
            "username" => $_POST["username"],
            "email" => $_POST["email"],
            "password" => $password,
        );
        $result = User::createUser($data);
        if($result === "ok"){
            Session::set("success","Account created");
            Redirect::to("login");
        }else{
            echo $result;
        }
    }
}

Verify passwords

To verify passwords we use the password_verify function that takes the user entered password and the stored one in the database.

                                                        
                                                                                                                        
<?php

class UsersController{
    //login the user
    public function auth(){
        if(isset($_POST["submit"])){
            $data["username"] = $_POST["username"];
            $result = User::login($data);
            if($result->username === $_POST["username"] && password_verify($_POST["password"],$result->password)){
                $_SESSION["logged"] = true;
                $_SESSION["username"] = $result->username;
                $_SESSION["fullname"] = $result->fullname;
                $_SESSION["admin"] = $result->admin;
                Redirect::to("home");
            }else{
                Session::set("error","No user found!");
                Redirect::to("login");
            }
        }
    }
    //store the user
    public function register(){
        $options = [
            "cost" => 12
        ];
        $password = password_hash($_POST["password"],PASSWORD_BCRYPT,$options);
        $data = array(
            "fullname" => $_POST["fullname"],
            "username" => $_POST["username"],
            "email" => $_POST["email"],
            "password" => $password,
        );
        $result = User::createUser($data);
        if($result === "ok"){
            Session::set("success","Account created");
            Redirect::to("login");
        }else{
            echo $result;
        }
    }
}

Related Tuorials

CRUD Application with PHP PDO Ajax, and MySQL Part 2

In the second part of this tutorial, we will get all the students from the database and display them...


CRUD Application with PHP PDO Ajax, and MySQL Part 1

In this tutorial we will see how to create a crud application with PHP PDO Ajax, and MySQL, the user...


How to Sort Associative Arrays in Descending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the key...


How to Sort Associative Arrays in Ascending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the key v...


How to Sort Associative Arrays in Descending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the valu...


How to Sort Associative Arrays in Ascending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the value...


How Do you Sort an Array in Descending Order in PHP

In this lesson, we will see how to sort descending an array in PHP, we will use the rsort() function...


How to Sort Ascending an Array in PHP

In this lesson, we will see how to sort ascending an array in PHP, we will use the sort() function t...


How to Remove a Key and its Value from an Associative Array in PHP

In this lesson, we will see how to remove a key and its value from an associative array in PHP,&nbsp...


How to Modify a Value in an Associative Array in PHP

In this lesson, we will see how to modify a value in an associative array in PHP, an Associative arr...