【问题标题】:Missing Password check缺少密码检查
【发布时间】:2010-12-28 21:51:44
【问题描述】:

我正在使用下面的代码,它会检查空字段并验证电子邮件,但即使密码正确也不会登录。密码已经插入md5保护,下面是代码。

PHP:

      session_start(); 

      //Checks if there is a login cookie

      if(isset($_COOKIE['ID_my_site']))


     //if there is, it logs you in and directes you to the members page

     { 
       $email = $_COOKIE['ID_my_site']; 

        $pass = $_COOKIE['Key_my_site'];

           $check = mysql_query("SELECT * FROM accounts WHERE email = '$email'")or              die(mysql_error());

        while($info = mysql_fetch_array( $check ))  

        {

           if ($pass != $info['password']) 

                    {

                          }

           else

             {

               header("Location: home.php");



               }

            }

        }


     //if the login form is submitted 

    if (isset($_POST['submit'])) { // if form has been submitted



     // makes sure they filled it in

           if(!$_POST['email'] | !$_POST['password']) {

           die('You did not fill in a required field.');

          }

          // checks it against the database



           if (!get_magic_quotes_gpc()) {

             $_POST['email'] = addslashes($_POST['email']);

          }

          $check = mysql_query("SELECT * FROM accounts WHERE email = '".$_POST['email']."'")or die(mysql_error());



        //Gives error if user dosen't exist

     $check2 = mysql_num_rows($check);

     if ($check2 == 0) {

               die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');

            }

            while($info = mysql_fetch_array( $check ))  

        {

         $_POST['password'] = stripslashes($_POST['password']);

     $info['password'] = stripslashes($info['password']);

      $_POST['password'] = md5($_POST['password']);



     //gives error if the password is wrong

        if ($_POST['password'] != $info['password']) {

          die('Incorrect password, please try again.');

        }

         else 

        { 


              // if login is ok then we add a cookie 

           $_POST['email'] = stripslashes($_POST['email']); 

           $hour = time() + 3600; 

              setcookie(ID_my_site, $_POST['email'], $hour); 

              setcookie(Key_my_site, $_POST['password'], $hour);     



                //then redirect them to the members area 

              header("Location: home.php"); 

          } 

           } 

        } 

     else 

  {  



      // if they are not logged in 



     <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 

      <table border="0"> 

    <tr><td colspan=2><h1>Login</h1></td></tr> 

    <tr><td>email:</td><td> 

    <input type="text" name="email" maxlength="40"> 

     </td></tr> 

       <tr><td>Password:</td><td> 

    <input type="password" name="password" maxlength="50"> 

    </td></tr> 

     <tr><td colspan="2" align="right"> 

    <input type="submit" name="submit" value="Login"> 

    </td></tr> 

  </table> 

   </form> 

}

这里是注册码:

PHP:

       // here we encrypt the password and add slashes if needed
        $_POST['password'] = md5($_POST['password']);
         if (!get_magic_quotes_gpc()) {
      $_POST['password'] = mysql_escape_string($_POST['password']);
     $_POST['email'] = mysql_escape_string($_POST['email']);
      $_POST['full_name'] = mysql_escape_string($_POST['full_name']);
     $_POST['user_url'] = mysql_escape_string($_POST['user_url']);
        }


        // now we insert it into the database
    $insert = "INSERT INTO accounts (Uniquer, Full_name, Email, Password, User_url)
  VALUES ('".$uniquer."','".$_POST['full_name']."', '".$_POST['email']."','".$_POST['password']."', '".$_POST['user_url']."')";
    $add_member = mysql_query($insert);

使用 ini_set 函数后,我看到了错误,我收到了这条消息,但不确定它的含义:

There are the lines where the errors are at: 

                if ($pass != $info['password']) 

还有这一行

                        if ($_POST['password'] != $info['password']) {

【问题讨论】:

    标签: php mysql database md5 verification


    【解决方案1】:

    我能看到的一件事是您在发布的密码(非 md5 版本)上使用 stripslashes,但在 md5 版本上使用它作为存储的密码。

    你不应该真的需要在数据库中的密码上使用stripslashes,因为密码不应该以斜杠开头。

    如果你改变了会发生什么:

    $_POST['password'] = stripslashes($_POST['password']);
    $info['password'] = stripslashes($info['password']);
    $_POST['password'] = md5($_POST['password']);
    

    收件人:

    $_POST['password'] = md5($_POST['password']);
    $_POST['password'] = stripslashes($_POST['password']);
    $info['password'] = stripslashes($info['password']);
    

    【讨论】:

    • 谢谢。我做了更改,我仍然无法通过。
    【解决方案2】:

    您尝试使用$info['password'] 访问db 字段,但数据库字段实际上是Password 大写P

    您正在调用mysql_escape_string 将数据存储在数据库中,并在提交的登录表单上调用stripslashes。也许他们对密码做了不同的事情。还有一些需要考虑的事情:

    • 如果您 md5 密码将其存储在数据库中,您不需要需要调用 stripslashesmysql_escape_string 就可以了。它已被转换为仅字母数字字符。
    • mysql_escape_string 已弃用。
    • 与其转义电子邮件字段,不如验证其格式。

    插入新注册后,手动 md5 使用的密码,然后根据存储在数据库中的密码检查该值。你可能会发现它们是不同的。

    【讨论】:

    • 感谢江铃。我测试了,我回显了输入的密码和数据库中的密码,它们与 md5 匹配。我仍在努力寻找它。无论如何,谢谢。
    • 如果您能帮我解决这个问题,我将不胜感激。我得出的结论是,问题可能出在 $info['password'] 上,我附和了它,但没有显示....
    • 在屏幕上打印错误后,我收到以下消息:注意:未定义的索引:第 103 行 /var/www/domain.com/htdocs/login.php 中的密码注意:使用未定义的常量密码 - 在第 11 行 /var/www/domain.com/htdocs/login.php 中假设为“密码”
    • @AAA 数据库字段大写“密码”,您正在使用`$info['password']`来访问它!
    • 我不知道为什么,但它仍然可以工作而无需更改它。大声笑疯狂的代码。
    【解决方案3】:

    你前段时间用完全不同的代码问了一个类似的问题,所以我假设它现在更像是一个数据库问题。

    您的密码字段是否允许 32 个字符用于 md5 哈希?太短的密码字段会截断 md5 哈希,因此总是无法登录。

    【讨论】:

    • 嘿,是的,我正在使用另一个代码,所以在这里转贴。密码最大长度设置为 50。
    • 并且db中的密码字段设置为varchar(40)
    • 信息['password']的用途是什么; ?
    • 它是你的代码......但在我看来 $info['password'] 是存储在数据库中的密码的哈希值,用于比较
    • @AAA 你能粘贴你用来在数据库中存储注册的代码吗?
    【解决方案4】:

    我解决了,如果您注意到查询中显示 SELECT *,我尝试选择电子邮件、密码。

    【讨论】:

    • 那根本不应该有任何区别。
    • 是的。但不知道为什么它会起作用,我不记得改变了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 2019-05-13
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    相关资源
    最近更新 更多