3

I have a PHP registration form that is very time consuming from when the user clicks on the submit button to the point that it will force the server to throw out the following error:

Request Timeout
This request takes too long to process, it is timed out by the server. If it should not be timed out, please contact administrator of this web site to increase 'Connection Timeout'.

I have been searching on google for ways to solve this issue and there seems to be a few ways of resolving this. however, I would like to know what is the best way of doing this.

I will explain the steps in the registration form from the when the user clicks the submit button:

step 1- it will checks the mysql database for duplicated data.
step 2- it will create 2 mysql tables with their columns
step 3- it will enter the data passed on by the user into the mysql table.
step 4- it will create a subdomain on my server.
step 5- it will move some files from one folder on my server into the newly created subdomain. there are alot of files but i don't think this should take that long? I need some advise on this please and what is the best way of doing this?
step 6- it will send a confirmation email to the user.

I can post my code but it is very big so I thought it might bore anyone's reading this question.

at the top of my PHP registration page, I have this line of code but this has no affect:

set_time_limit(0);

do you think it is best to use the following code instead?

ini_set('max_execution_time', 300);

any advise to solve this issue would be appreciated.

Thanks

user3592614
  • 99
  • 3
  • 13
  • 2
    The best way to solve it is to start by measuring the time taken by each of the different steps – Mark Baker May 19 '14 at 09:55
  • @MarkBaker, is there any way to give the server a "rest" lets say after the step 3 and then restart the process? I used the PHP's `sleep()` function it didn't work> – user3592614 May 19 '14 at 10:00
  • Giving the server a rest is a great way of hitting that timeout while simply sleeping..... the best approach is to spawn this as a background task that isn't constrained by timeout; but if you do run it from a web page, you need to know what actions are taking a long time to execute – Mark Baker May 19 '14 at 10:01
  • @MarkBaker, I am running this from a web page and I suspect the file moving in step 5 or email sending in step 6 will take the longest time. – user3592614 May 19 '14 at 10:05
  • What about splitting the entire script into multiple scripts (pages). Like a batch process or so? – fortune May 19 '14 at 10:07
  • @fortune, that sounds interesting. however, I have never done that. any examples by any chance? – user3592614 May 19 '14 at 10:09
  • 1
    see you are just inserting the registration data into table registration with a field (status) = value (new). Inform the customer that he will receive a detailed email shortly. There is another script which will run at regular intervals like a cronjob (every 1 min) or so fetching the rows having status=new and do the necessary processing and finally reach at step 6 – fortune May 19 '14 at 10:16
  • @fortune, great explanation. the only thing that I need to know is if this process won't overload the server? for example if 20 users are registering at the same time. – user3592614 May 19 '14 at 10:27
  • 1
    no it wont. you can even split each steps into different php scripts based on the table field status. Say if you have completed first step you can update the status value to "checking duplicates completed". Now start the second step by fetching rows having status = "checking duplicates completed".And do the second step. Once its completed you can append the status = "mysql tables created" and so on. – fortune May 19 '14 at 10:33

2 Answers2

8

This particular error message is generated by the LiteSpeed web server (a faster replacement for Apache). It has a special feature whereby it cancels long-running scripts. To disable this, you need to put the following in your .htaccess file in the root of your site.

RewriteEngine On
RewriteRule .* - [E=noabort:1]
RewriteRule .* - [E=noconntimeout:1]
Simon East
  • 55,742
  • 17
  • 139
  • 133
3

It looks pretty much as what hosting servers do.

If you have a process that has some "long" steps then the common practice is to split it and do the "long" steps in the background by a cron job.

So try doing following: let steps 1,2,3 pass as expected, but lock the user account after that and give him/her a message like "Your account will be unlocked soon. We will send you a follow-up email".

After that, write a console script (it can be PHP or any other language) that fetches all the new users that have registered but did not yet passed the whole routine and does the steps 4,5,6 for each of them.

Finally write a cron job that executes the script every minute.

P.S.: The splitting may be different. As @Mark_Baker mentioned it is better first to set time limit to 0 and measure the time for each step.

Community
  • 1
  • 1
Ruslan Bes
  • 2,715
  • 2
  • 25
  • 32