Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.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 实现密码兼容_Php_Validation_Authentication_Pdo_Cryptography - Fatal编程技术网

Php 实现密码兼容

Php 实现密码兼容,php,validation,authentication,pdo,cryptography,Php,Validation,Authentication,Pdo,Cryptography,我已经用这个库正确地实现了散列的创建,只改变了成本值。现在我被困在实际验证用户上。对于我目前的系统,我还必须检查用户是否被激活,然后启动会话。这就是我所拥有的: <?php require('core/init.php'); // Required field names $required = array('username', 'password'); // Loop over field names, make sure each one exists and is not

我已经用这个库正确地实现了散列的创建,只改变了成本值。现在我被困在实际验证用户上。对于我目前的系统,我还必须检查用户是否被激活,然后启动会话。这就是我所拥有的:

 <?php 
require('core/init.php');

// Required field names
$required = array('username', 'password');

// Loop over field names, make sure each one exists and is not empty
$error = false;
    foreach($required as $field) {
        if (empty($_POST[$field])) {
        $error = true;
        }
    }
//This is what will make the password 
$form_password = $_POST['password'];
$hash = password_hash($form_password, PASSWORD_BCRYPT);
    if ($error) {
        echo "Please Check username and password field!";
    }
        else { 
                if (password_verify($form_password, $hash)) {
                    $member_username = $_POST['username'];
                    $query = $dbh->prepare("SELECT * FROM users WHERE username = :user");
                    $query->bindParam(':user', $member_username);
                    $query->execute();
                    $row = $query->fetch();

                        if($row['activated'] > 0){
                            $_SESSION["user_id"] = $row['user_id'];
                            header("location: login_success.php");

                            }   else {
                                    echo "Account not activated wait for system administrator!";

                                }


                } else {
                            Echo "Wrong password or username please <a href='index.php'><bold>Retry!</bold></a>";

                        }


        }
我所做的是运行一个查询,如果密码验证实际上是真的。然后,该查询获取激活的行,以检查该值是否大于0,如果不大于0,则用户未被激活

然而,我的错误是,无论我输入什么,我总是得到错误的密码或用户名,请重试

我知道密码和用户没有错误。我在猜测自己是否正确实现了password\u compat的验证部分

任何帮助都会很好。多谢各位

编辑:

开发者在评论中回答的问题

问题是:我没有意识到我不能使用库$password和$hash变量作为全局变量。为了解决这个问题,我创建了变量,一切正常。谢谢大家!

代码中未定义$password和$hash。至于$password,您可以从$\u POST获取,但$hash来自数据库,所以您必须在验证密码之前查询它:

if ($error) {
    echo "Please Check username and password field!";
} else {
    $member_username = $_POST['username'];
    $form_password = $_POST['password'];
    $query = $dbh->prepare("SELECT * FROM users WHERE username = :user");
    $query->bindParam(':user', $member_username);
    $query->execute();
    $row = $query->fetch();
    if (!$row || !password_verify($form_password, $row['password'])) {
        if ($row['activated'] > 0) {
            $options = array(/* Your current hashing options */);
            $algorithm = PASSWORD_BCRYPT;
            if (password_needs_rehash($row['password'], $algorithm, $options)) {
                $hash = password_hash($row['password'], $algorithm, $options);
                /* Store new hash in db */
            }
            $_SESSION["user_id"] = $row['user_id'];
            header("location: login_success.php");
        } else {
            echo "Account not activated wait for system administrator!";
        }
    } else {
        Echo "Wrong password or username please <a href='index.php'><b>Retry!</b></a>";
    }
}

$password和$hash从何而来?这是一个库,它不设置任何全局变量。我是问你是否知道$password和$hash在你的代码中都没有定义。这不是一个真正的问题,它将被关闭。这是一个疏忽或缺乏一般理解,不是一个真正的问题。。。除此之外,它对整个社区也没有任何帮助,也没有建设性。@RaGe10940对你的具体问题没有任何暗示。但这句话是有缺陷的。相信我:有很多愚蠢的问题。@RaGe10940抱歉,但现在我真的不知道你在做什么,但我觉得你完全错了……至少在这一部分,你读得对。但是它没有告诉你在验证你正在做的事情之前创建一个新的散列;verify函数接受两个参数。用户提供的密码和用户记录中保存在数据库中的散列。正如我所说,你完全错了。有了这样的代码,用户可以使用任何密码登录。散列应该计算一次,存储在db中,并在我回答时检索以进行验证。现在,您正在为提交的密码计算哈希,并立即检查它,而不与任何用户关联。@RaGe10940:基本上您需要做的是:1。从数据库中检索哈希。2.使用从db获得的哈希值和用户在verify函数的表单中提供的密码。3.当提供的密码有效时,运行哈希通过密码\u需要\u重新哈希。4.如果密码需要重新哈希,请像用户注册密码时那样再次哈希,并将新哈希保存在database@RaGe10940当用户在您的站点上注册时,将根据当前最安全的设置创建哈希。然而,随着时间的推移,计算机变得越来越快,哈希算法也被破坏。为了确保存储的哈希值与此保持一致,您需要确保保存的哈希值保持最新。一个简单的例子是,目前在PHP中使用的最安全的东西是bcrypt。不过,我可以看到PHP支持更安全的scrypt。