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.

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 following code to the PHP script:

Ini_set ('max _ execution_time ', '0'); // Set the running time to 0 (unlimited );

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 and php is modified. ini script execution time limit, edit php. ini, modify the max_execution_time value:

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: 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 script Time limit: 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. if you have 10000 data records and want to modify some of the data, you can use PHP to perform step-by-step processing. the code is as follows: action. php

  1. $ Stid = isset ($ _ GET ['stid'])? $ _ GET ['stid']: 0;
  2. $ Endid = $ stid + 100;
  3. $ Maxid = 10000;
  4. Function dosomething (){
  5. // Operations that take a long time
  6. ......
  7. }
  8. $ SQL _string = "select * from 'table' where id> '$ stid' and id <=' $ endid' order by id ";
  9. $ Datas = getdata_bysql ($ SQL _string );
  10. Foreach ($ datas as $ data ){
  11. // Process data
  12. .....
  13. Echo $ id. "processing completed. ";
  14. If ($ id >=$ maxid) {exit ;}
  15. }
  16. If ($ stid <= $ maxid ){
  17. $ Stid =$ stid + 100;
  18. $ Url = "action. php? Stid = $ stid ";
  19. Echo $ url;
  20. Echo' Location = "'. $ url.'"; script ';
  21. }
  22. ?>

Dosomething () is a time-consuming operation. here we limit the id range to reduce the running time. after the operation is completed, the next step is automatically run 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: 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.