“Keep Me Logged In” - the best approach

后端 未结 12 1787
Happy的楠姐
Happy的楠姐 2020-11-22 08:30

My web application uses sessions to store information about the user once they\'ve logged in, and to maintain that information as they travel from page to page within the ap

12条回答
  •  天命终不由人
    2020-11-22 09:05

    I think you could just do this:

    $cookieString = password_hash($username, PASSWORD_DEFAULT);
    

    Store $cookiestring in the DB and and set it as a cookie. Also set the username of the person as a cookie. The whole point of a hash is that it can't be reverse-engineered.

    When a user turns up, get the username from one cookie, than $cookieString from another. If $cookieString matches the one stored in the DB, then the user is authenticated. As password_hash uses a different salt each time, it is irrelevant as to what the clear text is.

提交回复
热议问题