Search code examples
phpsessioncookiessession-cookiesremember-me

Cookie and session in "remember me" feature


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?


Solution

  • 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).