Ted
Ted

Reputation: 3875

How to store user data in a session?

I would like to populate my $_SESSION array with all DB data of the relevant user that just logged in.

like "FIRST_NAME" in the DB would be $_SESSION["FIRST_NAME"];

What is the way to do that properly ?

 $_SESSION = $result_set; //$result_set is the fetched array from the DB

Yielded an error (unknown() at line 0)

Upvotes: 0

Views: 325

Answers (2)

Kirk Beard
Kirk Beard

Reputation: 9843

You need to specify a name for the data you're storing - you can't overwrite the $_SESSION variable completely.

EG:

$_SESSION['user_id'] = $result_set->id;

Just as Pekka said, there's rarely the need to store the entire user profile in the session when you can do a separate database call to gather this data later.

Upvotes: 1

Pekka
Pekka

Reputation: 449395

I would like to populate my $_SESSION array with all DB data of the relevant user that just logged in.

Don't do this. Store only the row ID in the session, and do a database request any time you need some information.

Storing this information in the session is completely redundant, and may lead to outdated data if the user changes something in the database that isn't reflected in the session variable.

Upvotes: 4

Related Questions