PHP - stopping concurrent users

I need a method to stop concurrent logins using PHP / MySQL.

Current setting

Currently, there are 2 people who use the same login in the system that I created inside, where I work. Due to the nature of the system, I do not want both of them to log into the system.

What i tried

About 10 articles on stackoverflow and googled as long as I can.

I also tried to add the "loggedin" field to the user table, which was set to 1 when logging in, and "0" was set when logging out. Then, if the same user credentials that are used when logging in will fail.

The problem was that if the person who logged in turned off the browser without logging out properly, he did not update the database. Then I get a phone call from this person, and I would have to reset the value 0 in the database.

This is not possible, since soon the product will be released to about 20 people.

What I need

I need to find a way that when the browser closes the script, it executes to update the database. Alternatively, a way to read every current session on the server that I could manipulate, or something else.

Limitations

Our hosting providers are terrible, and subsequently the changes that I can make on the server are limited / impossible. Hosting is a public hosting solution.

+8
php mysql login session user-management
source share
5 answers

I need to find a way that when the browser closes the script, it executes to update the database.

You can not.

A better solution might associate a session identifier with a user in a database. If the session identifier on the user's computer does not match the latest session identifier in the database, force it to log in and update db.

+6
source share

maybe you should flip this.

the person trying to log in gets a new session, and the new session cancels any old sessions for the same username (thus writing them down)

Thus, only one person can enter the system at a time.

+3
source share

There is no reliable way to do this by the very nature of HTTP. But if you can handle some delay time, there is a solution.

The best you could do is create a JS script by pushing the "keep-alive" script to your server. Do it every 30 seconds or so. If your server has not heard from the client for 1 minute, close the session.

+1
source share

You can use the loggedin field, but adding another loggedin_timestamp field. Then you check when the user is logged in for the last time.

If this is longer than the session save time, you allow it to register again

0
source share

I used a jquery script delay session session to log out automatically. script will call back the PHP script with which we can change the database session timestamp. So, I think this link will help you. Jquery timeout

0
source share

All Articles