tags:

views:

68

answers:

3

Is it best to store all the users information in session cookies like this on login? Instead of using alot of querys

$_SESSION['id'] = mysql_result($result, 0, 'id');
$_SESSION['name'] = mysql_result($result, 0, 'name');
$_SESSION['email'] = mysql_result($result, 0, 'email');
$_SESSION['ip'] = mysql_result($result, 0, 'regip');
$_SESSION['groupid'] = mysql_result($result, 0, 'group_id');
$_SESSION['style'] = mysql_result($result, 0, 'style');

Or maybe just store ID in session then query on all page:

select * from users where id = $_SESSION['id']
+2  A: 

If you're going to be using them a lot, sure. Although I'd be careful about storing all of the users data in the Session. Only what is necessary. Keep in mind that well-written queries are cheap on resources. Don't try to avoid connecting to the database altogether, just be sure that when you do it, you do it well.

You can test your memory-consumption if you like using memory_get_usage(). But note that session data is stored in a flat file on the system itself:

session_start();
echo memory_get_usage(); // 87744
$_SESSION["user"] = array(
    "name"    => "Jonathan Sampson",
    "id"      => 54680,
    "groupID" => 0285,
    "email"   => "[email protected]",
    "style"   => "background-color:#333;color:#f1f1f1"
);
echo memory_get_usage(); // 88344

So you can see that it's really very trivial to store this amount of data.

See also: Cache data in PHP SESSION, or query from db each time?

Jonathan Sampson
I agree. I usually just save the `UserID`, and use that (+ other environmental variables) to get the user's details from the database.
Douwe Maan
+1: In my opinion, the only information that should be a candidate for sessions is that which is required for every single page. Otherwise, go back to the database as needed.
Chris Lively
But what if the user changes their email address? The developer has to remember to amend the session var. If you only save the user id but fetch the rest from the db each time, you don't need to worry about this.
adam
Exactly adam. If you are providing your users the ability to change the data, then you probably shouldn't store it in session.
Jonathan Sampson
good answer! gracias!
Jamal
de nada, Jamal :)
Jonathan Sampson
I been caching like 10 values for each user in file based session, I didn't realize it was not good, it seems like such a small amount of data. Maybe I should re-think this
jasondavis
+1  A: 

Why not use something like this:

$_SESSION['user'] = mysql_fetch_assoc($result);

(Not really an answer to your question, should have been a comment, I'm sorry)

Douwe Maan
You'd have to serialize it first, as it's an array
adam
Nope, Sessions can store arrays too.
Douwe Maan
-1 This has nothing to do with the users question. Should be a comment if anything.
Jonathan Sampson
ahh, my mistake on the serialize thing
adam
I just realized it isn't really an answer to the OP's question, but I think it should be noted nonetheless. Maybe a comment to the original post would've been better.
Douwe Maan
"Don't call your query resource $result" - It's the result resource, why not call it $result? What would be a better name?
VolkerK
Well, when someone says `result`, I think of the resultset, which is not what `mysql_query()` returns. I have no idea on a better name, really...
Douwe Maan
But it returns a resource that let's you access the resultset.
VolkerK
Yeah, but the resource also lets you access other things.I take back my advice on not calling it `result` btw, as I just saw PHP.net uses that term too... Call it whatever you like ;-)
Douwe Maan
+2  A: 

I'd create a (salted) hash for each user, saved in the db:

$salt = 'asdjnvdskflds';
$hash = md5($salt . $email);

Then store the hash in the session. That way, you can call the user data each time. This also has the benefit of not having to update the session if the user changes some of his/her data.

adam