Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6025547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:18:59+00:00 2026-05-23T04:18:59+00:00

Question is easy and basic. I’ve been working with PHP sessions for years and

  • 0

Question is easy and basic. I’ve been working with PHP sessions for years and I always managed user login/logout this way:

  1. Start session (session_start() call).
  2. Login: Store a value in the session (i.e. $_SESSION["user_id"] = 34).
  3. Check user logged: Check session value (i.e. isset($_SESSION["user_id"])).
  4. Logout: destroy session (session_destroy() call and unset($_SESSION["user_id"])).

This scheme has worked for me with very easy applications, but now I’m working in a bigger application and this approach is a bit problematic. For instance, I’m not able to implement the “remember” checkbox in the login box, because I’m able to set a bigger session cookie expiration date, but the session ends sooner ($_SESSION["user_id"] not set).

The point is, how can improve this scheme or which is the standard scheme to manage user sessions in PHP?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:18:59+00:00Added an answer on May 23, 2026 at 4:18 am

    Your approach is very fine for normal sessions. The problematic bit here is a “remember me” functionality, which needs to be handled differently than a normal session.

    A common way to implement that functionality is to store a second cookie with a far expiration date and put the user ID plus a secure hash in it. You need the user id or some other identification to detect which user comes back, but you also need the secure hash to be sure that the cookie is the one that your web app set and has not been crafted manually.
    If you do not have a secure hash, people can sent a self-built cookie with the user ID and automatically get logged in.

    So the secure hash needs to contain information that only your web app knows about, i.e. the user creation date.

    You might want to do it like this:

    $cookieValue = (int)$user->id . ':' . md5($user->creationDate . '/' . $user->passwordHash);
    

    Since neither creationDate nor passwordHash change, you can verify the validity of the secure hash when the user tries to login via cookie. When the user changed his password, the password hash changes and the user needs a new cookie – which is very fine in my eyes, since people who stole the cookie would also be logged out.

    If you want additional security, use another value for hash creation, i.e. a special cookie hash that you store along with the other user data. You can create it completely randomly and should change it whenever the user logs in:

    $randomValue = md5(time() . rand() . $user->passwordHash);
    $user->setCookieValue($randomValue);
    $cookieValue = (int)$user->id . ':' . $randomValue;
    

    Now when logging in:

    list($userId, $hash) = explode(':', $cookieValue);
    $user = loadUser($userId);
    if ($user instanceof User && $user->cookieValue == $hash) {
        //user logged in
        //generate and set new cookie value
    } else {
        // handle invalid persistent cookie
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a basic PHP question, and I'm trying to learn the
This is a very basic question with probably an easy answer. Let's say I
this is might be a very basic question, but seems like it's not easy
Easy question this time. I'm trying to test whether or not a string does
Hopefully an easy question, but I'd quite like a technical answer to this! What's
Maybe this is an easy question, maybe not. I have a select box where
This is probably an easy question, but I want to understand better how Apache
I think this is an easy question, but my googling is weak on this.
I guess this question of mine is pretty basic but since Ive never done
This might be a basic CSS question: I have a site designed with well-defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.