apatik apatik - 2 months ago 23
PHP Question

Warning: Illegal string offset (on an array)

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

}

Answer

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:

Comments