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.
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: |
Copy code |
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: |
Copy code |
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: |
Copy code |
<? Php // 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: |
Copy code |
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.