Home  >  Article  >  Backend Development  >  Solution to PHP script execution timeout_PHP tutorial

Solution to PHP script execution timeout_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:57:15930browse

The default script execution timeout in PHP is 30 seconds. If you do not set it, it will time out after 30 seconds if your script has not been executed. Let me explain in detail how to solve the PHP script execution timeout. .

The default maximum execution time in php.ini is 30 seconds. Although the value of max_execution_time in php.ini can be adjusted to achieve the goal, in some cases there is no condition to modify php.ini. How to solve this problem? .

One way is to add

in your PHP script
The code is as follows Copy code
 代码如下 复制代码

ini_set('max_execution_time', '0');

ini_set('max_execution_time', '0');

Set the running time to 0 (infinite value);

Another method is to execute the script from the command line. When executing the script from the command line, the maximum running time is set to unlimited.

Modify the script execution time limit of php.ini

Edit php.ini and modify the max_execution_time value:
 代码如下 复制代码

max_execution_time=500

The code is as follows Copy code

max_execution_time=500

//This modification requires reloading php.ini and restarting the web server to take effect.

 代码如下 复制代码

php_value max_execution_time 500

Set script execution time through .htaccess file

The code is as follows Copy code
php_value max_execution_time 500
 代码如下 复制代码

ini_set('max_execution_time', 500);

Set the maximum execution time in the script

The code is as follows Copy code
 代码如下 复制代码

set_time_limit(0);

ini_set('max_execution_time', 500);
Use php function to cancel the time limit of the script
The code is as follows Copy code
set_time_limit(0);

set_time_limit is used to set the timeout of the script. This function stipulates that the program must run to completion within the specified number of seconds from the time the sentence is run. If the timeout expires, the program will exit with an error.

The following is an example. There are 10,000 pieces of data. To modify some of the data, use PHP to perform the processing step by step. The code is as follows:

action.php

function dosomething(){
//Operations that require a lot of time
……
}
$sql_string=”select * from `table` where id>’$stid’ and id<=’$endid’ order by id”;
$datas = getdata_bysql($sql_string);
foreach($datas as $data){
//Process data
…..
echo $id.” Processing completed.
”;
if($id>=$maxid){exit;}
}
if($stid<=$maxid){
$stid = $stid + 100;
$url=”action.php?stid=$stid”;
echo $url;
echo ‘’;
}
?>

The code is as follows
 代码如下 复制代码

$stid = isset($_GET['stid'])?$_GET['stid']:0;
$endid = $stid + 100;
$maxid = 10000;

function dosomething(){
//要时间比较多的操作
……
}
$sql_string=”select * from `table` where id>’$stid’ and id<=’$endid’ order by id”;
$datas = getdata_bysql($sql_string);
foreach($datas as $data){
//处理数据
…..
echo $id.” 处理完成.
”;
if($id>=$maxid){exit;}
}
if($stid<=$maxid){
$stid = $stid + 100;
$url=”action.php?stid=$stid”;
echo $url;
echo ‘’;
}
?>

Copy code

$stid = isset($_GET['stid'])?$_GET['stid']:0;
$endid = $stid + 100;
$maxid = 10000;

The dosomething() is a time-consuming operation. Here we reduce the running time by limiting the id range. After running, the next step is automatically run through the javascript jumpThis is what dedecms does now when generating html pages. http://www.bkjia.com/PHPjc/632105.htmlwww.bkjia.com
true
http: //www.bkjia.com/PHPjc/632105.html
TechArticleThe default script execution timeout in php is 30 seconds. If you do not set it after 30 seconds, if your The script will time out before it is executed. Let me explain in detail how to solve PHP script execution...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn