0

I am using PHP and MySQL to accept form registrations. Most responses to this question assume that only 1 person is using the application at once. However, any number of people could be completing the form at one time. How do I limit the number of registrants to a given number (1000, for example)? Please consider the following scenario:

  1. 2:00 pm - User A accesses the form. My code checks the database, and there are 999 entries.
  2. 2:01 pm - User B accesses the form. My code checks the database, and there are 999 entries. (User A has not submitted the form yet).
  3. 2:08 pm - User A submits the form. Now, there are 1000 entries. User B is still filling out the form.

Since User B has already started filling out the form (but has not finished), I'd like to let him/her finish. What is the best way to code my application to minimize the likelihood that this scenario will occur?

Thank you.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • i cna think of a few ways but it depends on what the form is and the project specifications, you could just stop at 990 to allow for this, use AJAX to check as soon as form filling begins, throw away the extra one or tow that may get .. –  Jan 11 '14 at 03:30

3 Answers3

0

It can be done easily with a database trigger before insert the form data you count the amount of registries that are on your database and if it reaches your limit, you return a controlled error.

The only problem for this is that the user will have to fill all form and submit.

On your page you just check if it reaches the limit to block the form. The ones who accessed the form before the limit will be blocked by the trigger (if multiple users has accessed the same time).

The database trigger will alow a registry at a time.

Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
0

That's a classic counting semaphore use case:

  • Initialize your semaphore with 1000.
  • If the semaphore is greater than 0 let a new user start filling out the form and decrease the semaphore, else don't.
  • When a user finished a form, increase the semaphore.

In this case you don't have to use the IPC semaphore (but you could). Just implement an own counter. Your semaphore might be a simple database query:

SELECT 1000 - count(*) FROM Registration WHERE state = "unfinished";
Markus Malkusch
  • 7,738
  • 2
  • 38
  • 67
0

As long as the number of registrants is less than your maximum, serve the form. Have an ajax call in a window.setInterval to a simple service on your server that checks the number of rows relative to that maximum in the table and returns a value that can be evaluated as true or false. Set the interval to 5 or 10 seconds. If the service responds with false, you can inform the user that registration is closed.

I know you said you'd like to let user B finish, even if his registration will fail, but that may lead to an irritated user that's wasted more time filling out the form when he could have been warned sooner.

J.D. Pace
  • 586
  • 1
  • 3
  • 17