耗时的PHP脚本和解决问题的方法


time consuming PHP script and the ways to solve the issue?

我有一个PHP注册表单,它非常耗时,从用户点击提交按钮到它将迫使服务器抛出以下错误:

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'.

我一直在谷歌上搜索解决这个问题的方法,似乎有一些解决这个问题的方法。但是,我想知道这样做的最好方法是什么。

我将从用户单击提交按钮开始解释注册表单中的步骤:

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.

我可以张贴我的代码,但它太大了,所以我想它可能会让任何人阅读这个问题。

在我的PHP注册页面顶部,我有这行代码,但这没有影响:

set_time_limit(0);

你认为使用下面的代码是最好的吗?

ini_set('max_execution_time', 300);

任何解决这个问题的建议都将是非常感谢的。

谢谢

这个特定的错误信息是由LiteSpeed web服务器(一个更快的Apache替代品)生成的。它有一个特殊的功能,可以取消长时间运行的脚本。要禁用此功能,您需要将以下内容放入站点根目录下的.htaccess文件中。

RewriteEngine On
RewriteRule .* - [E=noabort:1]
RewriteRule .* - [E=noconntimeout:1]

看起来很像托管服务器的工作。

如果您的进程有一些"长"步骤,那么通常的做法是将其拆分,并通过cron作业在后台执行"长"步骤。

所以试着这样做:让步骤1、2、3按预期通过,但在此之后锁定用户帐户,并给他/她一个消息,如"您的帐户将很快解锁。"我们会给你发一封后续邮件。

之后,编写一个控制台脚本(可以是PHP或任何其他语言),获取所有已注册但尚未通过整个例程的新用户,并为每个用户执行步骤4、5、6。

最后编写一个cron作业,每分钟执行一次脚本。

注。分裂可能会有所不同。正如@Mark_Baker所提到的,最好先将时间限制设置为0,然后测量每一步的时间。