Home > DeveloperSection > Forums > Illegal string offset warning using session
jayprakash sharma
jayprakash sharma

Total Post:117

Points:821
Posted on    November-01-2014 12:00 AM

 ASP.Net PHP  Session 
Ratings:


 1 Reply(s)
 883  View(s)
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 better understanding can explain it better.



Tanuj Kumar

Total Post:134

Points:940
Posted on    November-01-2014 12:01 AM

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.


Don't want to miss updates? Please click the below button!

Follow MindStick