密码验证帮助(php新手)

时间:2017-07-09 19:56:10

标签: php mysql forms post mysqli

我目前刚接触PHP,而且我正在学习加密。我使用密码哈希加密我的密码,但我似乎无法验证,如果你给我一些帮助,我真的很感激。谢谢。以下是我的注册表。

Login.php

// Include database connection file
include_once('inc/connection.inc.php');
if (isset($_POST['Login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $result = $db - > query("select * from users where username='$username'");
    $row = $result - > fetch_array(MYSQLI_ASSOC);
    if (password_verify($password, $row['password'])) {
        $_SESSION['logged'] = 1;
        $_SESSION['username'] = $row['username'];
        header("location:index.php");
    } else {
        echo "error";
    }
}

Signup.php

session_start();
// Include database connection file
include('inc/connection.inc.php');
// Check to see if the form has been submitted
if (isset($_POST['register_btn'])) { // Check to see all fields have been completed
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $storedpassword = password_hash($password, PASSWORD_DEFAULT);
    // Create an SQL query to add the user
    $query = "INSERT INTO users (username, password, email, first_name,
    last_name) VALUES('$username', '$storedpassword', '$email',
    '$first_name', '$last_name')";
    $result = $db - > query($query);
    if ($result) {
        header("Location: login.php");
    }
    $db - > close();
}

Connection.inc.php

{
    $db = new mysqli('localhost', 'kzaman', '1010901', 'kzaman_test');
    if (mysqli_connect_errno()) {
        echo 'Error: Could not connect to database.  Please try again later.';
        exit;
    }
}

0 个答案:

没有答案