Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我的密码哈希不起作用,登录时会运行我的else代码_Php - Fatal编程技术网

Php 我的密码哈希不起作用,登录时会运行我的else代码

Php 我的密码哈希不起作用,登录时会运行我的else代码,php,Php,当我注册时,在我的登录页面中使用密码\u散列时,我遇到了一个问题。密码被散列,但当我登录时,它会运行代码中的else部分 这是我的登录代码 function login(){ global $db, $username, $errors, $disable, $hashed_password; $username = mysqli_real_escape_string($db, $_POST['username']); $password = mysqli_real_e

当我注册时,在我的登录页面中使用密码\u散列时,我遇到了一个问题。密码被散列,但当我登录时,它会运行代码中的else部分

这是我的登录代码

function login(){
    global $db, $username, $errors, $disable, $hashed_password;

    $username = mysqli_real_escape_string($db, $_POST['username']);
    $password = mysqli_real_escape_string($db, $_POST['password']);


    if (count($errors) == 0) {
        $password = password_verify($password, $hashed_password);

        $query = "SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1";
        $results = mysqli_query($db, $query);

        if (mysqli_num_rows($results) == 1) {
            $logged_in_user = mysqli_fetch_assoc($results);
            if ($logged_in_user['user_status'] == 'Active') {
                $_SESSION['user'] = $logged_in_user;
            }else{
                array_push($disable, "Your Account has been disabled");
            }
        }else {
            array_push($errors, "Wrong username/password combination");
        }

    }
}
下面是我散列密码的部分

$hashed_password = password_hash($password, PASSWORD_DEFAULT);

使用准备好的语句,然后从数据库中获取哈希密码,并将其与
password\u verify()
中的
$\u POST['password']
进行匹配

一般来说,不鼓励使用
global
——您应该将参数传递给函数

function login(){
    global $db, $errors, $disable;

    if (isset($_POST['username'])) {
        $stmt = $db->prepare("SELECT id, password, user_status FROM users WHERE username=?");
        $stmt->bind_param("s", $_POST['username']);
        $stmt->execute();
        $stmt->bind_result($user_id, $hashed_password, $user_status);

        if ($stmt->fetch() && password_verify($_POST['password'], $hashed_password)) {
            if ($user_status == 'Active') {
                $_SESSION['user'] = $user_id;
            } else {
                $disable[] = "Your Account has been disabled";
            }
        } else {
            $errors[] = "Wrong username/password combination";
        }

        $stmt->close();
    }
}

login
函数不包含此行。您可以详细说明如何调用它吗?您应该将值作为参数传递给登录函数,而不是使用全局变量。
password\u verify
返回true或false。为什么要将其注入SQL?我只是新加入PHP:'(如果我将其更改为PDO,我是否需要重新编码我所有的网站或只是注册和登录功能?你需要更改整个网站。你可以继续使用MySQLi,但你应该使用准备好的语句…查看答案谢谢它可以工作,但由于我是PDO的新成员,我找不到在哪里放置
$logged\u in\u user=MySQLi\u fetch\u assoc($results)
因为我看不到你的代码PDO没有方法
mysqli\u fetch\u assoc()
,那就是mysqli。也就是说,你不在代码中使用
mysqli\u fetch\u assoc()
,因为它使用的是一个预先准备好的语句,这比普通的
查询()
。我的答案中的函数是完整的函数。我如何在我的数据库中回显
user\u type
$\u SESSION['user']=$user\u id;
它应该就在这里?因为我有代码,它将根据用户的角色重定向他们页面中的用户。您可以使用用户名设置会话,即
$\u SESSION['user']=$\u发布['username']
(或从数据库中获取它,以便获得正确的大小写),或在页面上运行查询,使用用户ID获取用户名。