tags:

views:

93

answers:

4

I have never wanted to allow a user to stay logged in for any length of time so I never saw a use for a "remember me" feature. I started thinking about how it's done though and would like some clarification.

I'm currently storing my sessions in a database. What has always perplexed me was how, even though I do not explicitly set a cookie, one is placed in my browser. I'm a little confused because a session is a session and a cookie is a cookie. I don't see how a session sets a cookie.

I'd also like to know, if simply setting another session variable in the session array to keep the user logged in would be sufficient or would I still need to set a cookie?

+2  A: 

In order to pull the session data back from your database a key is needed. This is called the Session ID.

The session ID needs to be stored somewhere. Either as part of the URL string that the client posts back or, more commonly, in a cookie on the client. When the request is posted, session reads the value from the cookie and knows which record to pull back from session storage.

This happens automatically.

The only reason to use session is if the data you want to keep is greater than 4KB (browser limitations); or if the time required to pull the data from your server is greater than reading it from session storage.

If the amount of data you are storing is less than 4KB I would highly recommend you just set that in the cookie to begin with. I generally store things like the user id, user first name, and a couple other attributes. Bear in mind that it is trivial to inspect a cookies value, so this information should be encrypted prior to going to the client.

Another thing is if the query time to pull the data you need from the original source is small, then elect to do that instead of placing it in session. That way you only get it when you actually need it instead of with every single page load.

Chris Lively
Thanks. I am currently saving the session id in the database. I have verified that the same session id in the cookie is the same as the one in the database. Do I still need to set a cookie or can I just add another value to the session array and check that way?
AlekT
See my updates. Typically on log on I store the user id in an encrypted cookie and don't use session at all. When I get the cookie back I know whether they have logged in or not.
Chris Lively
While I'm thinking about it Chris, let me ask you another qiestion. I noticed yesterday that I was able to alter one of my cookies currently on my browser. Isn't this a security issue? Now, on the encrypted cookie, is there already a function that does this or will I need to roll my own?
AlekT
Residuum, here is what I currently know and may be where I'm failing to understand. My database holds the session ids for all logged in users. When a user returns, I can check to see if the cookie that my session store set on their computer; if its set then they can continue else go back to the login page. What am I missing?
AlekT
For php look at http://php.net/manual/en/function.mcrypt-encrypt.php On 01-dec-2006 davidwhthomas wrote a simple example of encrypting / decrypting cookies.
Chris Lively
BTW, altering cookies is not considered a security issue. Basically there is no way of knowing whether you can trust what's on the client side (or between you and it) anyway. Which is why you should always encrypt the cookies.
Chris Lively
+1  A: 

What has always perplexed me was how, even though I do not explicitly set a cookie, one is placed in my browser.

A session handler has to identify which session belongs to which user.

The vast majority of session libraries do this by setting a cookie.

(Is) setting another session variable in the session array to keep the user logged in would be sufficient or would I still need to set a cookie?

Most session libraries set session cookies. These are cookies without a specified expiry time. They expire when the browser closes and are not sufficient to implement a "Remember Me" feature (which is expected to persist across browser restarts, so must have an explicit expiry time).

David Dorward
OK, so I will need to set a cookie then. I can use the same session id though for the cookie, right? I mean, the session is saved in the database already, I just need to evaluate the client cookie and proceed from there. Am I right?
AlekT
Having sessions hanging around for weeks is going to make a very big, very inefficient session store. When designing your Remember Me feature, don't even think about about sessions. Use a different cookie name.
David Dorward
Thanks Dave.. If I use a different cookie name, I'll still store the cookie id?? in the database, right?
AlekT
Having said all that, sessions still have an expiry time. In PHP, this is controlled by the session.cache_expire setting, which defaults to 180 minutes.
R. Bemrose
A: 

Explaining the relationship between Cookie and Session:

PHP uses Cookie to uniquely identify the session for each user. That's the only more reliable way because cookie is sent each time you request a file from the server. Using the token in the cookie, which is also the Session identifier, PHP will look up the tmp directory to see if the session exists. If the session exists, the variables are loaded from the correct file and you will be able to access the variables on that session.

Therefore, cookies store the session identifier which is required to identify which user uses which session. This is also how Session Hijacking come about, when people can change the session identifying cookie to use another person's session identifier.

thephpdeveloper
Thanks! So, if I understand correctly, PHP will automatically use the same session id for the cookie, correct?
AlekT
A: 

The underlying PHP session implementation sets the cookie. You can alter this and have the session ID value passed in the query string, but I don't recommend it. You don't use the cookie, PHP does. It references the session ID value stored in the cookie to perform lookups to session data.

I'd also like to know, if simply setting another session variable in the session array to keep the user logged in would be sufficient or would I still need to set a cookie?

As soon as the user closes the browser, the session is killed and the cookie is deleted. I don't believe any mechanism exits to persist the session value, and for good reason.

bdl
To be more specific: The cookie is deleted and the session times out. On the server side, you cannot detect, that the browser closed and "destroy" the session.
Felix Kling