PHP登录验证

时间:2013-08-23 13:40:03

标签: php

常量只包含数据库连接详细信息     

require_once 'includes/constants.php';
class mysql{
    private $conn;

    function __construct(){
        $this->conn = $conn = new MySQLi(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME)
            or die ('There was an error in the connection');
        }

    function verify ($un, $pwd){


             $query = "SELECT * FROM User WHERE username = ? AND pass = ? LIMIT 1"; 

             if ($stmt = $this->conn->prepare($query))
             {
                 $stmt->bind_param('ss', $un, $pwd);
                 $stmt->execute();

                 if ($stmt->fetch());{
                     $stmt->close();
                     return true;
                     }
             }
        }

    }

?>
从登录页面传递了un和pwd,它们是从用户输入中提取的,我遇到的问题是用户总是登录,无论他们输入什么,它似乎都没有正确验证数据库

2 个答案:

答案 0 :(得分:0)

更改此

if ($stmt->fetch());{
   $stmt->close();
   return true;
}

 if ($stmt->fetch()){
   $stmt->close();
   return true;
}else
{ return false;}

答案 1 :(得分:0)

if ($stmt->fetch());{
                   ^------- THIS!
    $stmt->close();
    return true;
}

if之后是空语句,之后会出现一个新的{...}个语句块。 if仅与空语句关联,因此始终执行以下块。

举例说明:

if (false)
    /* do nothing */;

{
    echo 'always goes here';
}

摆脱多余的;