-1

I'm having a hard time trying to figure out why I am getting the following error :

Warning: Illegal string offset 'username'

on this code :

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');
}

If I try to echo it I will get twice the error (the second time on the echo line).
I really don't understand what is going wrong, especially since I am using the $errors array several times in my code and this is the only block causing me troubles.

Here is the sample of code this block (at the bottom) is from :

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');
    }

}
apatik
  • 383
  • 4
  • 17
  • 1
    The variable `$errors` is not an array - it already holds another value and PHP doesn't want to implicitly create an array in its place. -- Probably due to `$errors = "access forbidden";` being called first – Michael Berkowski Sep 07 '16 at 23:54
  • 1
    `$errors = "access forbidden";` --- it's a string. – zerkms Sep 07 '16 at 23:54
  • Wow, I understand now... $error as a variable has never been declared before until a user with an unactivated account tried to log in before activating an account (which doesn't have "r" in his permissions list), so it triggered the first function... Is that correct ? Anyways I'm going to verify that, Thanks a huge lot this was bugging me for an hour already and seeing how tired I am it could have gone much longer before noticing it. Thanks a lot ! – apatik Sep 07 '16 at 23:59

1 Answers1

1

You are mixing types.

Early in the script you use $errors as a string.

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');
}

Then later you use it as an array:

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

For example, consider the following script:

<?php

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

PHP Warning: Illegal string offset 'x' in /path/file.php on line n

Further Readings:

Gerard Roche
  • 6,162
  • 4
  • 43
  • 69
  • Indeed that was the case. I had never caught it before because the condition where $errors is a string had never been triggered before. Thank you for the full explanation on the matter ! – apatik Sep 08 '16 at 00:14