Home  >  Article  >  Backend Development  >  How to solve php running timeout

How to solve php running timeout

angryTom
angryTomOriginal
2019-08-24 09:27:4611315browse

How to solve php running timeout

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 give you some instructions. Detailed explanation of how to solve PHP script execution timeout.

Recommended manual:php complete self-study manual

Recommended tutorial:PHP video tutorial

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

ini_set('max_execution_time', '0');

to the PHP script to set the running time to 0 (infinite value);

 Another The first 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
//此修改需要重新加载php.ini,需要重启web服务器生效。

Set the script execution time through the .htaccess file

php_value max_execution_time 500

Set the maximum execution time in the script

ini_set('max_execution_time', 500);

Use the PHP function to cancel the time limit of the script

set_time_limit(0);

set_time_limit is used to set the timeout time of the script. This function specifies the time limit from the script. When the sentence is run, the program must be completed within the specified number of seconds. If it times out, 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

<?php
 $stid = isset($_GET[&#39;stid&#39;])?$_GET[&#39;stid&#39;]: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.” 处理完成. <br />”;
  if($id>=$maxid){exit;}
 }
 if($stid<=$maxid){
 $stid = $stid + 100;
 $url=”action.php?stid=$stid”;
 echo $url;
  echo ‘<script language=”javascript”>location=”‘.$url.’”; </script>’;
 }
?>

Among them dosomething() is a time-consuming operation. Here we reduce the running time by limiting the id range, and automatically run the next step through javascript jump after running.

This is how dedecms generates html pages now oh.

Recommended related articles:
1.What is the way to make PHP timeout
2.How to solve the PHP request interface timeout
Related video recommendations:
1. Dugu Jiujian (4)_PHP video tutorial

The above is the detailed content of How to solve php running timeout. For more information, please follow other related articles on the PHP Chinese website!

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
Previous article:Types of cache in phpNext article:Types of cache in php