避免PHP执行时间限制 [英] Avoid PHP execution time limit
问题描述
我需要用PHP语言创建一个执行数字置换的脚本.但是PHP的执行时间限制为60秒.我该如何运行脚本,以便如果您需要运行60个以上的sesunde,而不会被服务器中断.我知道我可以在php中更改最大执行时间限制,但是我想听听另一个不需要事先知道脚本执行时间的版本.
I need to create a script in PHP language which performs permutation of numbers. But PHP has an execution time limit set to 60 seconds. How can I run the script so that if you need to run more than 60 sesunde, not been interrupted by the server. I know I can change the maximum execution time limit in php, but I want to hear another version that does not require to know in advance the execution time of a script.
一个朋友建议我经常登录并从服务器注销,但是我不知道该怎么做.
A friend suggested me to sign in and log out frequently from the server, but I have no idea how to do this.
欢迎任何建议.示例代码将很有用.
Any advice is welcome. An example code would be useful.
谢谢.
首先,我需要输入一个数字,比方说25.启动脚本后,它需要执行以下操作:对于每个< =大于25的数字,它将创建一个文件,其文件编号为目前阶段;对于下一个数字,它将打开之前创建的文件,并在打开的文件的行上创建另一个文件,依此类推.因为这要花很长时间,所以我需要避免脚本被服务器干扰.
推荐答案
@emanuel:
我猜想,当您的朋友告诉您一个朋友建议我经常从服务器登录并注销,但我不知道如何执行此操作."时,他/她一定是说将您的脚本计算拆分为x件作品并分别运行"
I guess when your friend told you "A friend suggested me to sign in and log out frequently from the server, but I have no idea how to do this.", he/she must have meant "Split your script computation into x pieces of work and run it separately"
例如,使用此脚本,您可以执行150次以达到150! (分解)并显示结果:
For example with this script you can execute it 150 times to achieve a 150! (factorising) and show the result:
//脚本名称:calc.php
// script name: calc.php
<?php
session_start();
if(!isset($_SESSION['times'])){
$_SESSION['times'] = 1;
$_SESSION['result'] = 0;
}elseif($_SESSION['times'] < 150){
$_SESSION['times']++;
$_SESSION['result'] = $_SESSION['result'] * $_SESSION['times'];
header('Location: calc.php');
}elseif($_SESSION['times'] == 150){
echo "The Result is: " . $_SESSION['result'];
die();
}
?>
顺便说一句(@Davmuz),您只能在Apache服务器上使用set_time_limit()函数,而在Microsoft IIS服务器上则无效.
BTW (@Davmuz), you can only use set_time_limit() function on Apache servers, it's not a valid function on Microsoft IIS servers.
这篇关于避免PHP执行时间限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!