Illegal string offset warning using session

Total Post:117

Points:821
PHP  Session 
 1800  View(s)
Ratings:
Rate this:

I'm trying to set session in my program, after the user has logged with his account. If user input good email and password, then program set some variables, like this:

$_SESSION['user']['id'] = $row['id'];
$_SESSION['user']['email'] = $email;
$_SESSION['user']['admin'] = $row['admin'];
$_SESSION['logged'] = true;

The problem is, that I'm getting this  warning from first three rows of code above:

Warning: Illegal string offset 'id' in C:\Program Files (x86)\Zend\Apache2\htdocs\OOPeshop\user\User.php on line 193

So I checked, if all the variables are set, so i add to my code this two lines:

var_dump($row);
var_dump($email);

but it was looking alright, I was getting output

array(2) { ["id"]=> string(2) "14" ["admin"]=> string(1) "0"} 

string(22) "foobar@gmail.com"

So can someone explain me where is the problem? I checked similar question, but found  no solution to my problem.

I have tried this:

$user = array('id' => $row['id'], 'email' => $email, 'admin' => $row['admin']);
$_SESSION['user'] = $user;

No more warnings. It set array first and then set whole array to session variable, it worked, but i don't know where's problem in my first code, so maybe someone with a better understanding can explain it better.

  1. Post:134

    Points:940
    Re: Illegal string offset warning using session

    I have tried this:

    $user = array('id' => $row['id'], 'email' => $email, 'admin' => $row['admin']);
    $_SESSION['user'] = $user;

    No more warnings. It set array first and then set whole array to session variable, it worked, but i don't know where's problem in my first code, so maybe someone with better understanding can explain it better.

    It seems that $_SESSION['user'] has been in somehow set to string, dont know why, but that's the only explanation.

      Modified On Apr-06-2018 02:11:01 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!