簡體   English   中英

CodeIgniter 3登錄類引發array_key_exists()期望參數2為數組,給定null

[英]CodeIgniter 3 Login Class Throws array_key_exists() expects parameter 2 to be array, null given

我們的CodeIgniter 3源代碼在應用程序/庫中具有Auth類,具有以下功能:

public function login($attempt) {
    $CI = & get_instance();
    $CI->load->model("users");

    // Store password entry
    $password = $attempt["password"];

    // Get user from database
    $user = $CI->users->get(array("screen_name" => $attempt["screen_name"]));

    if (!array_key_exists("password", $user)) {
        return null;
    }

    // Validate password
    $valid = crypt($password, $user["password"]) == $user["password"];
    if ($valid) {
        // Remove password - user data will be stored in session 
        unset($user["password"]);

        // If no photo uploaded, set default
        if (!isset($user["photo"]) || $user["photo"] == null) {
            $user["photo"] = "http://oururl/ourroute/assets/images/DefaultAvatar.png";
        } else {
            //$user["photo"] = "files/images/" . $user["photo"];
        }
        return $user;
    } else {
        return false;
    }
}

起初,我認為必須將返回值從false更改為null。但是,更改返回值后錯誤仍然存​​在。 由於null值和false返回值都不能緩解這種情況,因此應采用其他方法來糾正當前的錯誤嗎?

問題是因為此行返回null而不是數組

$user = $CI->users->get(array("screen_name" => $attempt["screen_name"]));

在將$ user傳遞給array_key_exists()函數之前,應驗證數據庫是否返回了有意義的結果。

那段代碼將這樣寫

if ($user == null || !array_key_exists("password", $user)) {
    return null;
}

這樣,您就不會將null值傳遞給array_key_exists

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM