PHP script Execution Time-out workaround _php Tutorial

Source: Internet
Author: User
The default script execution time-out in PHP is 30 seconds, if you haven't set it up for 30 seconds and if your script has not finished executing it will time out, let me give you a big explanation of how the PHP script executes the timeout.

The default maximum execution time in php.ini is 30 seconds, although it can be achieved by adjusting the value of max_execution_time in php.ini, but in some cases there is no condition to modify php.ini, how to solve this problem.

One way is to include in the PHP script

The code is as follows Copy Code

Ini_set (' max_execution_time ', ' 0 ');

Set the run time to 0 (infinite value);

Another method is to execute the script at the command line, and when executing the script using the command line, the maximum run time is set to an infinite value.

Modify the script execution time limit for php.ini

Edit PHP.ini, modify the Max_execution_time value:

The code is as follows Copy Code

max_execution_time=500

This modification requires a reload of php.ini, which requires a restart of the Web server to take effect.

Setting script execution time through. htaccess files

The code is as follows Copy Code

Php_value Max_execution_time 500

Set the maximum time to execute in a script

The code is as follows Copy Code

Ini_set (' Max_execution_time ', 500);

Time limit for canceling scripts with PHP functions

The code is as follows Copy Code

Set_time_limit (0);

The set_time_limit is used to set the time-out for the script, which specifies that the program must run at the end of the specified number of seconds from the runtime, and the program exits with an error.

Here is an example. There are 10,000 data, to modify some of the data, the use of PHP steps to perform processing, the code is as follows:

action.php

copy code

"!--? php
$stid = isset ($_get[' Stid ']) ? $_get[' Stid ']:0;
$endid = $stid + 100;
$maxid = 10000;

Function dosomething () {
//To take more time to operate
...
}
$sql _string= "select * from ' table ' where id> ' $stid ' and id<= ' $endid ' ORDER by id ';
$datas = Getdata_bysql ($sql _string);
foreach ($datas as $data) {
//processing data
...
echo $id. "Processing is complete.
";
if ($id >= $maxid) {exit;}
}
if ($stid <= $maxid) {
$stid = $stid +;
$url = "Action.php?stid= $stid";
Echo $url;
Echo ';
}
?

One of the dosomething () is a time-consuming operation. Here we can reduce the run time by restricting the range of IDs, and run the next step with JavaScript jump after running.

Now Dedecms is doing this when it comes to generating HTML pages.

http://www.bkjia.com/PHPjc/632105.html www.bkjia.com true http://www.bkjia.com/PHPjc/632105.html techarticle The default script execution time-out in PHP is 30 seconds, if you haven't set it up for 30 seconds, if your script has not finished executing it will time out, let me give you a big explanation of PHP script execution ...

  • 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: info-contact@alibabacloud.com 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.