Proper way to use “Remember me” functionality in PHP

后端 未结 4 530
礼貌的吻别
礼貌的吻别 2021-02-04 05:46

Short

Working on login system and trying to implement remember me feature.

Recently, l did research about this subject, read bunch of articles, posts, stories, n

4条回答
  •  我寻月下人不归
    2021-02-04 05:54

    To set time delay between login attempts (to prevent bruteforce attacks) and to limit attempts count

    So you're providing a method for DOS by account?

    To regenerate session id on nearly every operation

    erm, no. That's actually likely to defeat the object. You should always generate a new id when the current id is expired or when the user is authenticated - otherwise leave it alone.

    But I really confused about my main problem: which way is proper, for "remember me" feature? to use cookies/session/database?

    Since you need to retain a token on the client, that means cookies (unless you fancy writing something really complicated using local storage). Since you don't want to expose data via the cookie / make forgery simple that means it should be a random value. And in order to reconcile the stored random value, that means storing data serverside - probably in a database since it must be possible to reference the data based on the user id or based on the random value.

    While you could just use a non-expiring (or very long lived) session, I'd stay away from this - the data will snowball - and it's a good idea to renew the session data once in a while.

    You also need to cater for the scenario where a user wants you to remember her on 2 different computers. If you only hold a single 'remember me' token for each account, then either you'll have to use the same value at both clients or delete the old token when you create a new one (i.e. user can only be remembered on one machine).

    please explain your idea on code. I can't understand clearly without code

    No. I get paid to write code; If you want me to write the code for you then you'll need to pay me. And the code will take up much more space and time than the description above.

提交回复
热议问题