Sketchy on sessions with cookies...

michaelsanford

Translator, Web Developer
I've got a bunch of PHP scripts that need to access a variable $username.

If I register $username as a session variable, I can use it throughout the site. The only thing is, I'd also like to save it to a cookie.

Can I safely save the session variable $username to a cookie also called username, or will that defeat the ideas of sessions.

For that matter, why should I bother using sessions at all, and not just use cookies everywhere since there's no difference in the way the variables are accessed (with the obvious exception of session_start() and session_register beginning the script...)

Thanks for any insight!
 
session level variables are stored as cookies unless the client has cookies turned off, in that case they are automaticly passed in a Query string appended to the URL. So the advantage of sessions is that variables will be tracked even if cookies are disabled. If you have load balancing going on, you may need to consider using a database for session variables, I'm just starting to delve into that myself.
 
Cool I hadn't thought of storing session information in a database (since i've already got a huge database, and some of the session variables store stuff like username....) but that answers me question. Thanks!
 
Back
Top