php登录系统中的未知行为

时间:2016-06-06 01:09:01

标签: php oop pdo php-5.5

我有这种方法来验证用户名和密码,但我遇到了奇怪的行为,我不知道为什么在user.php中这样做:

public static function verify_user($username,$password)
{
    global $database;


    $sql = "select * from users where username= :username and password= :password";
    $result = $database->query($sql);


    $database->bind(':username', $username);
    $database->bind(':password', $password);
    $row=$database->single();   

    //* Create Array to put the objects inside it
    $the_object_array=array();

    //*Create an instance of the class and put inside the_object_array[]
    $the_object_array[]=self::instant_class($row);

    pretty_print(false);


    return !empty($the_object_array) ? array_shift($the_object_array) : false;



}


private  static function  instant_class($the_record)
{
     //* here we instantiate the User class it self using
    //* self keyword
     $the_object=new self;

    // $the_object->id=$found_user['id'];

     foreach ($the_record as $the_attribute => $value) {

        if($the_object->has_the_attribute($the_attribute)){
            $the_object->$the_attribute=$value;
        }
     }
     /* reset pointer, start again on step one*/
     reset($the_record);
     return $the_object;

}

我在login.php中有这个:

<?php


// if ($session->is_signed_in()) {
//  redirect("index.php");
// }


if (isset($_POST['submit'])) {


    $username=trim($_POST['username']);
    $password=trim($_POST['password']);

    //*Method to check DB user
    $userfound=User::verify_user($username,$password);
    //pretty_print($userfound);


    if ($userfound) {

        $session->login($userfound);
        //redirect("index.php");
        //pretty_print($userfound);


    }else{
        $the_msg="Your username or password are incorrect";
        // pretty_print($userfound);

    }
}
else{

    //$the_msg="";

    $username="";
    $password="";
}


 ?>


<div class="col-md-4 col-md-offset-3">

<h4 class="bg-danger"><?php echo $the_msg; ?></h4>

<form id="login-id" action="" method="post">

编辑得更清楚:

  • database.php:
public function bind($param, $value, $type = null){
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                $type = PDO::PARAM_INT;
                break;
                case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
                case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
                default:
                $type = PDO::PARAM_STR;
            }
        }

        try {
            $this->stmt->bindValue($param, $value, $type);

        } catch (Exception $e) {
            echo 'Exception -> ';
            var_dump($e->getMessage());
        }

    }

public function single(){
        try {
            $this->execute();
            return $this->stmt->fetch(PDO::FETCH_ASSOC);

        } catch (Exception $e) {
            echo 'Exception -> ';
            var_dump($e->getMessage());
        }

    }

主要错误是:  当用户输入错误的用户名或密码错误时,我无法获取此消息$the_msg="Your username or password are incorrect";

,此屏幕截图中的错误是:

  

警告:在第104行的C:\ xampp \ htdocs \ gallery \ admin \ includes \ user.php中为foreach()提供的参数无效

     

警告:reset()要求参数1为数组,布线在第111行的C:\ xampp \ htdocs \ gallery \ admin \ includes \ user.php中给出

所以结果就像这个截图,我不知道为什么?

enter image description here

1 个答案:

答案 0 :(得分:0)

您的foreach循环可能会发生两件事。要么没有从您的PDO语句返回记录,要么您的PDO excute()fetch()执行错误输出,single()返回您的try / catch错误消息。

鉴于您收到reset() expects parameter 1 to be array错误消息,它肯定是第一个(您的PDO语句没有返回任何内容)。 <{1}}在找不到任何内容时返回fetch()