PHP script execution timeout Solution

Source: Internet
Author: User

In php, the default time-out period for script execution is 30 seconds. If you do not set the time-out period to 30 seconds, if your script has not been executed, it will time out, next I will explain how to solve the PHP script execution timeout.

Php. the default execution time in ini is 30 seconds, although php can be adjusted. max_execution_time value in ini to achieve the goal, but in some cases, php is not allowed to be modified. ini, how to solve this problem.

One way is to add

The Code is as follows: Copy code

Ini_set ('max _ execution_time ', '0 ');

Set the running time to 0 (unlimited value );

Another method is to execute the script under the command line. When the script is executed using the command line, the maximum running time is set to an infinite value.

Modify the execution time limit of php. ini scripts

Edit php. ini and modify the max_execution_time value:

The Code is as follows: Copy code

Max_execution_time= 500

// You need to reload php. ini and restart the web server to make the modification take effect.

Set the script execution time through the. htaccess File

The Code is as follows: Copy code

Php_value max_execution_time 500

Set the maximum execution time in the script

The Code is as follows: Copy code

Ini_set ('max _ execution_time ', 500 );

Cancel the script time limit using php Functions

The Code is as follows: Copy code

Set_time_limit (0 );

Set_time_limit is used to set the script time-out time. This function specifies that the program must end in the specified number of seconds from the time of running the sentence. If the time-out occurs, the program exits with an error.

The following is an example. There are 10000 pieces of data. to modify some of the data, use PHP to perform step-by-step processing. The Code is as follows:

Action. php

The Code is as follows: Copy code

<? Php
$ Stid = isset ($ _ GET ['stid'])? $ _ GET ['stid']: 0;
$ Endid = $ stid + 100;
$ Maxid = 10000;

Function dosomething (){
// Operations that take a long 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. <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> ';

Dosomething () is a time-consuming operation. Here we limit the id range to reduce the running time. After running, we automatically run the next step through the javascript jump.

This is what dedecms does when generating html pages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.