Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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 密码\u散列无法正常工作_Php - Fatal编程技术网

Php 密码\u散列无法正常工作

Php 密码\u散列无法正常工作,php,Php,我正在使用我在另一个网页上使用过的代码,在另一个网页上可以正常工作,但在这个新网页上不行。用于登录的MySQLi表的设置也完全相同。我认为这不是登录脚本,因为我将另一个站点的密码哈希插入到数据库中进行测试,我可以使用它登录 这是注册码: include('sql.php'); $username = $_POST['username']; $password = $_POST['password']; $confirm = $_POST['confirm']; if($username ==

我正在使用我在另一个网页上使用过的代码,在另一个网页上可以正常工作,但在这个新网页上不行。用于登录的MySQLi表的设置也完全相同。我认为这不是登录脚本,因为我将另一个站点的密码哈希插入到数据库中进行测试,我可以使用它登录

这是注册码:

include('sql.php');
$username = $_POST['username'];
$password = $_POST['password'];
$confirm = $_POST['confirm'];

if($username == '' || $password = '') {
    header('Location:/register.php');
}
if($password != $confirm) {
    header('Location:/register.php');
}
$sql = "INSERT INTO login (username, password) VALUES ('" . $username . "', '" . password_hash($password, PASSWORD_DEFAULT) . "')";
if(mysqli_query($mysqli, $sql)) {
    header('Location: /dashboard.php');
} else {
    echo $mysqli->error;
}
mysqli_close($mysqli);
和登录名:

session_start();
$error = '';

if(isset($_POST['submit'])) {
    if(empty($_POST['username']) || empty($_POST['password'])) {
        header("Location: /admin.php?error=invalid");
    } else {
        include('sql.php');
        $username = mysqli_real_escape_string($mysqli, stripslashes($_POST['username']));
        $password = mysqli_real_escape_string($mysqli, stripslashes($_POST['password']));
        $sql = "SELECT * FROM login WHERE username='" . $username . "'";
        $result = $mysqli->query($sql);
        if($result->num_rows == 1) {
            while($row = $result->fetch_assoc()) {
                $verify = password_verify($password, $row['password']);
                if($verify == false) {
                    header("Location: /admin.php?error=mismatch");
                } else {
                    $_SESSION['login_user'] = $username;
                    $_SESSION['login_pass'] = $password;
                    if($_POST['stay'] == 'stay') {
                        setcookie('username', $username, time() + 31536000, '/');
                        setcookie('password', $password, time() + 31536000, '/');
                    }
                    header("location: /dashboard.php");
                }
            }
        } else {
            header("Location: /admin.php?error=mismatch");
        }
        mysqli_close($mysqli);
    }
}

嗯,这不应该像以前那么难发现

如果$username==| |$password={header'位置:/register.php'}

特别是$password=


这是一个赋值,不是相等检查。将其更改为$password==我敢打赌它会工作。

说。了解。即使是这样也不安全!我推荐PDO,我希望它比使用非参数化查询更简单、更干净、更安全。@PatrickQ是的,它们确实如此。很好。被它蒙蔽了,理论上,这个问题应该作为一个重复的问题来结束。我只是想知道是否有人会因为你没有把它改写成一个事先准备好的声明或者他们说的话而否决你;让它们接受sql注入。Sigh@Fred-哈哈。我确实不想在里面加一些空格,因为这是我个人的偏好:而且,我也不一定反对这个问题被关闭。我只是想确定OP确实看到了错误所在。是的,没错。在所有的凝视和评论的时间之后,你应得的,是赞成的/接受的是LOL!不是dv的。