The Set_time_limit function in PHP is used to limit the execution time of a page, as I want to define the execution time of a PHP page as 5 Seconds to Set_time_limit (5).
A PHP script executes every 5 minutes through crontab, taking into account that the script executes more than 5 minutes, deliberately using set_time_limit (290) to control the script to exit in 290 seconds. One day suddenly found that there are multiple processes in the background that the script is executing, which means that set_time_limit (290) does not work. To prove this, deliberately use the following code to test.
| The code is as follows |
Copy Code |
Set_time_limit (5); for ($i = 0; $i < $i + +) { echo Date (' y-m-d h:i:s '). "N"; Sleep (1); } |
The script does not exit after 5 seconds, either on the Web or the CLI. Later added Ini_set (' Max_execution_time ', 5) test, the same result. Does that mean the Set_time_limit function is useless at all? Actually, in the http://stackoverflow.com/questions/5874950/set-max-execution-time-in-php-cli here to find the root, in fact, is the above the wording of the problem, For example, use the following code:
| The code is as follows |
Copy Code |
Set_time_limit (5); for (;;) { } |
After execution, it is possible to see "Fatal error:maximum Execution time of 5 seconds exceeded in" for about 5 seconds like this error. Indicates that Set_time_limit is working. Now take a look at the description of this function on the official document (http://www.php.net/manual/en/function.set-time-limit.php), written in note:
The Set_time_limit () function and the configuration directive max_execution_time only affect the execution time of the SCR IPT itself. Any time spent on activity that happens outside the execution of the script such as system calls using System (), Stream op Erations, database queries, etc. is isn't included when determining the maximum time and the script has been running. This is not true on Windows where the measured time is real.
Cases
| The code is as follows |
Copy Code |
Set_time_limit (0); $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. " has been deposited into the database, number: ". $phpid; } $i--; echo ""; } ?> |
Note: The time that the sleep function is paused is also not counted as the execution time of the script. So that's why the first Test failed.
When your page has a lot of data, it is recommended to use Set_time_limit () to control the run time, the default is 30s, so you need to take the time to add a long point, such as Set_time_limit (300), where the number of seconds set to 0, indicating continuous operation!
such as: Set_time_limit (0) indicates long-time link run!
Note: This function requires you to turn off Safe mode, set Safe_mode = off Safe mode to off in php.ini, or the following error will occur:
Warning:set_time_limit () [Function.set-time-limit]: Cannot set time limit in Safe mode in
Again, note that:
In php.ini, you can set the maximum execution time for PHP pages by defining max_execution_time, such as the following:
| The code is as follows |
Copy Code |
Set_time_limit (900);
|
This function specifies the maximum execution time for the current PHP script,
Although the set value is 900 seconds, actually
Maximum execution time max_execution_time value in =php.ini-the time the current script has been executed + setpoint
If max_execution_time=30 in php.ini, the current script has been executed for 10 seconds, then:
Maximum execution time =30-10+900=920 seconds.
The workaround for setting set_time_limit in PHP does not work:
The set_time_limit is used to set the time-out of the script as follows:
Set_time_limit (number of seconds);
Specifies that the program must run at the end of the specified number of seconds from the time the sentence is run.
Timeout the program error exits.
However, sometimes setting set_time_limit does not work, the Set_time_limit function is best performed under Linux, and Windows execution may not be valid
Workaround:
Modify the Max_execution_time = 30 in the php.ini. This default is 30 seconds, modified to Max_execution_time = 300. Restart the Apache server. The timeout is set to 300 seconds and there is a message.
http://www.bkjia.com/PHPjc/628681.html www.bkjia.com true http://www.bkjia.com/PHPjc/628681.html techarticle The set_time_limit function in PHP is used to limit the execution time of a page, as I want to define the execution time of a PHP page as 5 Seconds to Set_time_limit (5). A PHP script through cron ...