views:

89

answers:

3

I'm trying to figure out if it is better to store my user's data in a session cookie (like password, username, etc), and update that cookie only when I change the MYSQL database from my PHP,

OR

Store the user's username and user ID in a session cookie and reach out to the MYSQL database every time I need to get the user's data.

Which one is the better method..? I've never actually set up a login system so any advice would be much appreciated.

+1  A: 

Good question. I've kept the user data in the PHP session. Since that's all server side it should be safe to do so, and avoids an extra database lookup.

The downside is that you won't automatically pick-up changes to the database that happen during that session... but user data isn't normally so dynamic that I'd care.

Chris Arguin
Any tips on updating the data...?
motionman95
+1  A: 

You shouldn't store a password in a cookie. I would store the details in the DB and use memcached to reduce the load on the DB.

George Marian
I'm guessing he means session variable rather than session cookie. The only thing stored in a session "cookie" is the session ID used to tie the client to a particular session. All other session data is stored server-side.
Lèse majesté
@Lèse majesté Possibly, I wanted to avoid any confusion, as his first option specifies "session cookie." So, I provided the general case. I would lean away from using sessions, to avoid the overhead of staring/resuming sessions and -- maybe more importantly -- avoid having to remember to resume a session wherever this data is necessary. Also, I do believe memcached provides more options for how that data is cached. It certainly provides a better solution for clustered web servers, where you'd have to store session data in a central DB, in order to have that data available to all servers.
George Marian
memcached is an excellent solution for large-scale applications, but it would be overkill for what the OP is trying to do, especially since he mentions this is the first time he is building such a system.
casablanca
@casablanca True, but IMO, you should start doing it right from the very beginning. :) Memcached is pretty simple to use.
George Marian
+2  A: 

A session and a cookie aren't the same. A session simply stores its session ID in a cookie (client-side) and all the session data on the server. I presume you really meant session where you used the word 'cookie'.

Moving ahead to answer your actual question, it's perfectly fine and safe to store most user details in the session. You should never need to store the password in a session though, since you use it only for authentication. Apart from that, it's ideal to cache frequently used user data (things that you may display on every page) in your session to save trips to the DB.

The concern that Chris mentioned - changes to user data - is almost non-existent, because user data will (should) not be modified by anyone except the user himself, in which case you can update the session along with the DB.

casablanca
+1 for going into more detail that I did.
George Marian