【问题标题】:why iam getting worng username and pwd for correct things in php为什么我会在 php 中使用正确的用户名和密码
【发布时间】:2012-03-07 03:58:58
【问题描述】:

这是我在数据库功能中的检查登录

function CheckLoginInDB($username,$password)
{
    if(!$this->DBLogin())
    {
        $this->HandleError("Database login failed!");
        return false;
    }          
    $username = $this->SanitizeForSQL($username);
    $pwdmd5 = md5($password);
    $qry = "Select name, email from $this->tablename where username='$username' and password='$pwdmd5' and confirmcode='y'";

    $result = mysql_query($qry,$this->connection);

    if(!$result || mysql_num_rows($result) <= 0)
    {
        $this->HandleError("Error logging in. The username or password does not match");
        return false;
    }

    $row = mysql_fetch_assoc($result);


    $_SESSION['name_of_user']  = $row['name'];
    $_SESSION['email_of_user'] = $row['email'];
    $_SESSION['user_name']=$row['username'];

    return true;
}

.................................................. ………………………………………………………………………………………………………………………………………… 这在 login.php 中被称为

if($fgmembersite->Login()) 

如果返回true,则用户将被引导到登录成功页面。 但我不知道为什么它总是返回 false!并显示错误“登录错误。用户名或密码不匹配”...

请帮忙。

【问题讨论】:

    标签: php


    【解决方案1】:

    编辑:我还注意到,在您的代码中,您调用了一种方法,但是,您向我们展示了另一种方法……我认为这是不对的。

    尝试将or die(mysql_error()) 添加到您的线路:

    $result = mysql_query($qry,$this->connection);
    

    变成:

    $result = mysql_query($qry,$this->connection) or die(mysql_error());
    

    我猜你的查询失败了。

    【讨论】:

      【解决方案2】:

      由于这种情况,如果您的查询由于语法错误而失败,您仍然会收到“登录错误”消息,这对于失败的查询来说是虚假的:

       if(!$result || mysql_num_rows($result) <= 0)
      

      改为:

       if (!$result) {
           echo mysql_error();
       }
       else if (mysql_num_rows($result) <= 0) {
          // error logging in, non-matching user/pass
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-28
        • 1970-01-01
        • 2015-05-31
        • 2021-05-04
        • 2023-02-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多