对于设置为占用一些时间的查询,您应该将一些自动化请求移动到组合中,如果您可以访问 linux 服务器,最好是 cronjobs。
使用 cronjobs,您可以在数据库中为特定值创建条目,链接到用户。如果挂起的查询已完成,cronjob 将启动,每 5 分钟执行一次查询。这将最大限度地减少用户需要坐在页面上直到完成的事实。因为您应该知道,用户离开活动页面的那一秒;所有活动的连接、查询等都将停止。
脚本完成后,在最后制作一条自定义消息发送给用户,让他们知道他们的过程已经完成。
您还应该知道,PHP 会从第 1 行到最后执行脚本;因此,例如,如果您的挂起在第 40 行;该脚本将位于该行,直到查询完成然后继续处理。
编辑:这只是为了给你指出我要达到的方向,而不应该像你看到的那样使用它。这只是一个标记示例
<?php
if (isset($_POST['ButtonToExecute']))
{
// Query to update a table in your database which an external PHP script will work with
// Example Table Structure:
/*
Username
State
*/
if ($state == 0)
{
// Then update state to 1 with username to the username for the query to take place on
}
else
{
// Warn user that their process will take longer to complete as their already is an active query in process
// but add them to a secondry pipeline which will be picked up on the next cronjob interval
}
}
?>
在你的 cronjob 中,可能有:
<?php
if ($state=='1')
{
// Execute your script
// After query execution update state to 2
// if state == 2 then insert custom message/email to send
}
?>
编辑你的 crontab:
yourpreferrededitor /etc/crontab
^ yourpreferrededitor 表示您的文本编辑器,无论是 nano 还是其他。
还有你的 cronjob 行:
* * * * * root /usr/bin/php /var/www/cron.php
^ 这是从我每天每一分钟都在不断运行的当前 cronjob 中获取的
Cronjob 将使用户能够离开页面,因为正如我上面提到的,用户离开脚本的那一刻......所有正在进行的进程都会停止。 cronjob 将持续运行,无需用户交互(除了他们发出初始请求)