警告:非法字符串偏移(在数组上)

时间:2016-09-07 23:52:46

标签: php

我很难弄清楚为什么会收到以下错误:

  

警告:非法字符串偏移'用户名'

关于此代码:

if (ggconf::get('signup_use_activation') && isset($user['permissions']) && $user['permissions'] == 'x'){
    $errors['username'] = "account not activated";   // <--- this line here
    gg::writeLog('auth bad - account not activated');
}

如果我试图回应它,我会得到两倍的误差(第二次在回声线上) 我真的不明白出了什么问题,特别是因为我在我的代码中多次使用$ errors数组,这是导致我麻烦的唯一一块。

以下是此块(位于底部)的代码示例:

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

    $user = gg::getUser($_POST['username']);

    if (isset($user['permissions']) && !strstr($user['permissions'], 'r') || ($user['username'] == 'guest' && ggconf::get('allow_guests') == false)){
        $errors = "access forbidden";
        gg::writeLog('auth bad - auth not activated');
    }

    if (!isset($_POST['username']) || $_POST['username'] == ''){
        $errors['username'] = "enter your username";
        gg::writeLog('auth bad - blank username field');
    }

    if (!isset($_POST['password']) || $_POST['password'] == ''){
        $errors['password'] = "enter your password";
        gg::writeLog('auth bad - blank password field');
    }

    if (isset($user['akey']) && $user['akey'] != '' && strpos($user['akey'], 'otp-') === false){
        $errors['username'] = "account not activated";
        gg::writeLog('auth bad - account not activated');
    }

    if(!$errors && $user['username'] == $_POST['username'] && gg::checkPassword($_POST['password'], $user['password'])){
        $this->loginUser($user);
    }

    if(!$user){
        $errors['username'] = "wrong username";
        gg::writeLog('auth bad - wrong username');
    }

    if(!$errors && $user['username'] == $_POST['username'] && gg::checkPassword($_POST['password'], $user['password']) == false){
        $errors['password'] = "wrong password";
        gg::writeLog('auth bad - wrong password');
    }

    if (ggconf::get('signup_use_activation') && isset($user['permissions']) && $user['permissions'] == 'x'){
        $errors['username'] = "account not activated";
        gg::writeLog('auth bad - account not activated');
    }

}

1 个答案:

答案 0 :(得分:0)

您正在混合类型。

在脚本的早期,您使用$errors作为字符串。

if (isset($user['permissions']) && !strstr($user['permissions'], 'r') || ($user['username'] == 'guest' && ggconf::get('allow_guests') == false)){
    $errors = "access forbidden";
    gg::writeLog('auth bad - auth not activated');
}

然后你将它用作数组:

if (ggconf::get('signup_use_activation') && isset($user['permissions']) && $user['permissions'] == 'x'){
    $errors['username'] = "account not activated";
    gg::writeLog('auth bad - account not activated');
}

例如,请考虑以下脚本:

<?php

$variable = "fizz";
$variable['x'] = "y";
  

PHP警告:非法字符串偏移&#39; x&#39;在第n行的/path/file.php中

进一步阅读: