Three ways to implement PHP at timed intervals
- 1. Windows Scheduled Tasks
- 2, the Linux script program
- 3, let the Web browser refresh regularly
Concrete implementation
1. Windows Planning Tasks
PHP rarely run on the win server, the implementation is no longer detailed, look at the online implementation of the principle is probably to write a bat script, and then let the window task to add execute this bat script.
2, the Linux script implementation
This is mainly used to crontab this command,
How to use:
Copy Code code as follows:
Crontab Filecrontab [-u user] [-u user] {-l |-r | e}
Description
Crontab is used to enable the user to execute the program at a fixed time or at a fixed interval.
Use Crontab to write the shell script, and then let PHP call the shell, this is the use of Linux features, should not be the language of PHP's own features
3, PHP implementation of scheduled Tasks
using PHP to refresh your browser requires several issues
PHP script Execution time limit, the default is 30m solution: Set_time_limit () or modify php.ini set Max_execution_time time (not recommended)
If the client browser is shut down, the program may be forced to terminate, workaround: Ignore_user_abort Even if the shutdown page is still running normally
If the program has been executing very likely to consume a lot of resources, the solution uses sleep to use the program to hibernate for a while and then in the execution
PHP timed Execution Code:
<?php
Ignore_user_abort ()//Turn off the browser, the PHP script can also continue to execute.
Set_time_limit (3000);//Through Set_time_limit (0) allows the program to execute indefinitely
$interval =5;//every 5s run
//Method 1--dead loop
do{
echo ' Test '. Time (). ' <br/> ';
Sleep ($interval);//wait 5s
}while (true)
; Method 2---Sleep timed execution
require_once './curlclass.php '//Introducing file
$curl = new Httpcurl ()//instantiating
$stime = $curl- >getmicrotime ();
For ($i =0 $i <=10; $i + +) {
echo ' Test '. Time (). ' <br/> ';
Sleep ($interval);//wait for 5s
}
Ob_flush ();
Flush ();
$etime = $curl->getmicrotime ();
Echo '
This is not very efficient when tested.
Summarize:
Personally feel that PHP time to perform the task is not very efficient, it is recommended that the task of regular execution or to the shell to do it.
The above is PHP time to perform the task of 3 methods, for the previous article at the end of the crontab This command, also carried a brief introduction, I hope we can have some harvest.