Phpset_time_limit () usage test _ PHP Tutorial

Source: Internet
Author: User
Tags exit in safety mode sleep function
Phpset_time_limit () usage Test details. In php, the set_time_limit function is used to limit the page execution time. for example, if you want to define the execution time of a php page as 5 seconds, you can set_time_limit (5. The set_time_limit function of a php script in php through cron is used to limit the page execution time. for example, if you want to define the execution time of a php page as 5 seconds, you can set_time_limit (5.

A php script is executed every five minutes through crontab. considering that the script execution time exceeds five minutes, set_time_limit (290) is used to control the script to exit in 290 seconds. One day, it was suddenly found that there were multiple processes executing the script in the background, that is, set_time_limit (290) did not work. Specifically, we use the following code for testing.

The code is as follows:

Set_time_limit (5 );

For ($ I = 0; I I <100; $ I ++ ){
Echo date ('Y-m-d H: I: s'). "n ";
Sleep (1 );
}

The preceding script does not exit in 5 seconds, whether on the web or CLI. Later, the ini_set ('max _ execution_time ', 5) test is added, and the result is the same. Does it mean that the set_time_limit function is useless at all? In fact, it is not in the http://stackoverflow.com/questions/5874950/set-max-execution-time-in-php-cli here to find the root, in fact, there is a problem with the above writing, such as the use of the following code:

The code is as follows:

Set_time_limit (5 );

For (;;){
}

After execution, you will see an error message similar to "Fatal error: Maximum execution time of 5 seconds exceeded in" in about 5 seconds. It indicates that set_time_limit works. Now go to the official document (http://www.php.net/manual/en/function.set-time-limit.php) about this function description, written in Note:

The set_time_limit () function and the configuration directive max_execution_time only affect the execution time of the script itself. any time spent on activity that happens outside the execution of the script such as system CILS using system (), stream operations, database queries, etc. is not supported Ded when determining the maximum time that the script has been running. this is not true on Windows where the measured time is real.

Example

The code is as follows:
// Set_time_limit (0 );
USD I = 1500;
Include ("inc/conn. php ");
While ($ I> 0)
{
$ SQL = "INSERT INTO php (php)
VALUES ('$ I ')";
If ($ conn-> execute ($ SQL) === flase)
{
// Echo "data insertion error". $ conn-> errormsg ();
}
Else
{
$ Phpid = $ conn-> Insert_ID ();
Echo $ I. "saved to the database, no.:". $ phpid;
}
$ I --;
Echo "";
}
?>

Note: The pause time of the sleep function is not counted in the script execution time. This is why the first test fails.

When your page contains a large amount of data, we recommend that you use set_time_limit () to control the running time. the default value is 30 s, so you need to extend the execution time, such as set_time_limit (300 ), set the number of seconds to 0, indicating continuous operation!

For example, set_time_limit (0) indicates that the link is running for a long time!

Note: To run this function, you need to disable the security mode. in php. ini, set the safety mode of safe_mode = Off to Off. Otherwise, the following error will occur:

Warning: set_time_limit () [function. set-time-limit]: Cannot set time limit in safe mode in

Note that:

In php. ini, you can define max_execution_time to set the maximum execution time of the PHP page. for example:

The code is as follows:
Set_time_limit (900 );

This function specifies the maximum execution time of the current php script,
Although the value is set to 900 seconds
Maximum execution time = max_execution_time value in php. ini-time when the current script has been executed + set value
If max_execution_time = 30 in php. ini and the current script has been executed for 10 seconds, then:
The maximum execution time is 30-10 + 900 = 920 seconds.

Solution for setting set_time_limit in php does not work:

Set_time_limit is used to set the script timeout time. the usage is as follows:

Set_time_limit (seconds );
It is required that the program must end in the specified number of seconds from the time of running the sentence,
The program exits after timeout.
However, sometimes setting set_time_limit does not work. it is recommended that the set_time_limit function be executed in linux, and windows execution may also be ineffective.
Solution:
Modify max_execution_time = 30 in php. ini. The default value is 30 seconds and is changed to max_execution_time = 300. restart the apache server. In this way, a message is displayed when the timeout value is set to 300 seconds.

Round (5. A php script uses cron...

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.