Remember Me from other places

I am trying to create a login system using PHP and MySQL. I use cookies to register users for a long time. A cookie has a randomly generated string that is also in the database. They are compared with each other and appropriate measures are taken. Basically, this is a standard authentication system.

My problem is that I'm not quite sure how to approach the authentication problem from several places. Since there is only one random line for each user, he will be logged out of one place if he logs in from another.

The only solution I can come up with is a table that stores multiple user logins, each of which has a separate random row.

Is this the right way? What happens to unused sessions?

+4
source share
1 answer

If you want to support multiple locations, you cannot store cookie values ​​in your user table. Rather, you should create a new autologin table using cookie values ​​and a user ID as primary and foreign keys, respectively. I think this is your idea.

Then, for business purposes, you can record the last time the last value was used to log in. Everything that happens during XX days is deleted, and the user needs to log in again. You can either set up a cron job for this, or run it every time someone signs up with a cookie.

The cron deletion task can handle more records (because no one is waiting for its completion), but for optimization you need an index in the date field (to prevent the table from being scanned).

Caring for the hosting at each login has the advantage that you only need to request cookie values ​​for this user, and due to the restriction of the foreign key, the search is quick. But you can clear the user's current cookies so that you can collect cookies that are never used again.

+3
source

All Articles