Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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_Mysql_Authentication_Login - Fatal编程技术网

Php 登录脚本未正确读取密码?

Php 登录脚本未正确读取密码?,php,mysql,authentication,login,Php,Mysql,Authentication,Login,我正在尝试创建一个登录脚本,但它似乎没有从我的数据库中正确读取密码,我不知道为什么 这是我的php代码: <? if ($_POST['login'] == "login") { include('db.php'); include('inc/scriptsandstyles.php'); include('inc/passwordLib.php'); $username = (isset($_POST['username'])? $_POST['usernam

我正在尝试创建一个登录脚本,但它似乎没有从我的数据库中正确读取密码,我不知道为什么

这是我的php代码:

<? 

if ($_POST['login'] == "login") {
  include('db.php');
  include('inc/scriptsandstyles.php'); 
  include('inc/passwordLib.php');

    $username = (isset($_POST['username'])? $_POST['username'] : null);
    $password = (isset($_POST['password'])? $_POST['password'] : null);

    $res = mysqli_query($conn, "SELECT user_name, user_password FROM rgitportal_users WHERE user_name='$username'");

    $row = mysqli_fetch_array($res);

    $pw_hash = $row['user_password'];

if (password_verify($password, $pw_hash)) {

        echo '<script type="text/javascript">window.location = "http://rgitportal.com/home.html"</script>';

} else {
    $errormsg = "Username or password incorrect";
}

}
?>
这是表格代码:

<form id="login_form"  name ="login" id ="login" action ="index.php" method = "POST">
    <div class ="field_container">
         Username: <input type="text" name ="username" id ="username"></input>
            </div>
    <div class ="field_container">
         Password: <input type="password" name ="password" id ="password"></input>
            </div>
    <div class ="field_container">
        <input type="submit" value="login" name ="login" id="login"></input>
            </div>
     <span style="color:#FF0000;"><?=$errormsg?></span>
 </form>

用户名:
密码:
如果我正确输入用户名和密码,它总是落在检查密码的If语句的else中。变量是从表单和相关数据库中正确分配的。数据库中保存密码的文本字段存储为密码,因此进行加密


我做错了什么?

我认为crypt()的第二个参数不应该是$hash。您在数据库中创建和存储密码时使用了什么salt?

这可能会有所帮助:

第一个示例显示,您应该使用hash_equals($hash_password,crypt($user_password[,$optional_salt])进行验证

(对于较旧的PHP版本,摘自):


请记住,crypt使用salt作为第二个参数,而不是散列密码


希望这能有所帮助。

尝试对代码进行以下更改:

主代码

if (password_verify($password, $pw_hash)) 
{
     header('Location:http://rgitportal.com/home.html');
} else 
{
    $errormsg = "Username or password incorrect";
}
passwordLib.php

if (!function_exists('password_verify'))
{
   function password_verify($password, $hash)
   {
       return (crypt($password) === $hash);
   }
}

'数据库中保存密码的文本字段存储为密码,因此被加密'。您是否正在使用数据库的
密码
功能?
if (password_verify($password, $pw_hash)) 
{
     header('Location:http://rgitportal.com/home.html');
} else 
{
    $errormsg = "Username or password incorrect";
}
if (!function_exists('password_verify'))
{
   function password_verify($password, $hash)
   {
       return (crypt($password) === $hash);
   }
}