I have a software (in cloud) with so many registered users.

I want to restrict the number of users logged in at a time to certain number (configuration based, let's say 100).

If the 101 user tries to login he should be placed in queue with priority 1, subsequently 102 user should be 2 in queue, I know at every time a logged in users request is hitting the server the sess_file access time will be updated.

What should be the logic from here? Should I check the last accessed time of all the sess_file and log-out the uses that are idle? and how will I manage the waiting queue?

Software is done with PHP-MySQL.

When the user is logged in I mark the column as logged in in user table, and when the log-in page is loaded if the total logged in user count is 100, the 101 user who is trying to log-in is directed to a intermediate page which will check via ajax call fired at an interval of 5 mins that if any user is logged out. If a slot is available it redirects to the log-in page, automatically logs in and redirects to the user dashboard. But I want to know any better practice is there.

Mostly I want to know the invalidation of logged in user by checking the access time of sess_file is the right way to do it?

  • Your idea sounds good. As a user, I can open another browser tab, and do other things while I'm waiting to log on. Most other websites just tell the user, "Sorry, server full." Commented Aug 28, 2013 at 14:03
  • Next to development you could take a look at some commercial solutions available: queue-it.com netprecept.com etc. They have those concepts available and it might also inspire you about features etc. Commented Aug 28, 2013 at 16:04
  • 1
    What is the particular reason for the need to do this in the first place? Commented Aug 28, 2013 at 16:18
  • @GrandmasterB: yes the business reason. the clients needs it like that. he should have the power to restrict the number of users at any given point of time
    – zamil
    Commented Aug 28, 2013 at 18:03

3 Answers 3


When the user is logged in I mark the column as logged in in user table, and when the log-in page is loaded if the total logged in user count is 100, the 101 user who is trying to log-in is redirected

I would alter that approach to allow all users to log in, but if the number of active users exceeds 100 then redirect the new user to a holding page. Their session is created but they are now in a queue to leave the holding page.

This makes giving users access a lot easier, because you no longer have to track the 100 logged in users. You can simply order by the record ID of session table. This approach assumes you are tracking sessions with a session table in your database. When a user logs out you delete their session record from the table.

It's assumed you are using an auto-increment integer for your ID column.

To check if the current user should be place on hold is very easy.

SELECT `id` FROM `sessions` ORDER BY `id` LIMIT 1;

Using the first id in the table you can quickly check if the user is in the top 100 sessions.

$id = query('SELECT `id` FROM `sessions` ORDER BY `id` LIMIT 1');
if($my_session_id > $id+100)
   // this user is placed in holding
   // this user has full access

As soon as a record above the current use is deleted. They automatically move up in the queue.

  • am doing the same thing. user details are validated and permission are set and keeping them in an intermediate page till the slot opens. but am not able understand what u said from there on
    – zamil
    Commented Aug 28, 2013 at 18:46

First off, put the session data in a database, don't rely on files. Once in the database, it'd be easy enough to track the number of active sessions with a simple query.

The main problem you are going to face is determining whether a user is idle or has closed their browser instead of manually logging out. You'll want some sort of 'keep alive' connection that tells your server the person is still logged in. A simple ajax request every 30 seconds or so should do the trick. If the server stops receiving that, you know to mark that session as logged out.

  • yes i am doing the same thing. from the parking page it sends an ajax request every 5mins to check the last modified date of sess_file files. but i am asking whether that is the best method to understand whether a user is idle?
    – zamil
    Commented Aug 28, 2013 at 18:43
  • Its not the same thing - I'm suggesting you keep the session data in a database so that its much simpler to query and count. But other than that, thats about the best you can do in a web app, unless you want to replace the ajax call with a long poll or websockets to get a more immediate notification if/when a browser closes. Commented Aug 28, 2013 at 18:56

My first ideia is to run a function when an user logs in, then increment the count +1, and then when the user logs out, decreases the count in -1. A single database field can work this out and make what you're proposing when the count reach 100 logged users!

I mean it's pretty simple actually and very usefull, send me your login and logout functions (session-based) and i can show you some ways to do that!

  • 2
    I'm not the down-voter, but the question is actually looking for something more sophisticated than how to count uses - how to handle the users who try to log in after the 100th user. Commented Aug 28, 2013 at 16:04
  • 1
    Welcome to Programmers William Castro! It seems as if you are a bit confused about how this site works. Try taking the tour to learn why our site is different, and how it is different than a forum. If you have any specific questions after going through the tour then feel free to ask a question on our Meta site. Thank you!
    – maple_shaft
    Commented Aug 28, 2013 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.