Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter 3登录类抛出数组\u key\u exists()期望参数2为数组,给定null_Php_Arrays_Codeigniter - Fatal编程技术网

Php CodeIgniter 3登录类抛出数组\u key\u exists()期望参数2为数组,给定null

Php CodeIgniter 3登录类抛出数组\u key\u exists()期望参数2为数组,给定null,php,arrays,codeigniter,Php,Arrays,Codeigniter,我们的CodeIgniter 3源代码在应用程序/库中有一个Auth类,具有以下功能: public function login($attempt) { $CI = & get_instance(); $CI->load->model("users"); // Store password entry $password = $attempt["password"]; // Get user from database $

我们的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;
}

这样做时,您不会将空值传递给array\u key\u exists

错误消息就是您的答案。
$user
为空正在使用什么身份验证库?发布您的
get
函数。看起来你得到的是一个空数组