Several methods of implementing timed tasks in PHP one. Simple and direct disregard of the consequence type
<?phpignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行.set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去ini_set(‘memory_limit‘,‘512M‘); // 设置内存限制$interval=60*30;// 每隔半小时运行do{ //ToDo sleep($interval);// 等待5分钟}while(true);
Disadvantage: After booting, you cannot control it unless you terminate the PHP host. Do not use this method unless you are a hacker.
Two. Simple and controllable type
config.php
<?phpreturn 1;?>
cron.php
Ignore_user_abort (); Turn off the browser and the PHP script will continue to execute. Set_time_limit (0); $interval =60*30; //runs every half hour do{ $run = include if (! $run) die ( ' process abort '); //todo Sleep ( $interval); while (true);
The STOP program is implemented by changing the return 0 of the config.php. One way to do this is to config.php a file with a special form and set some variables to configure it via HTML pages.
Disadvantages: Accounting for system resources, long-running, there will be some unexpected hidden dangers. For example, memory management problems.
Three. Simple and improved type
<?php$time=15;$url="http://".$_SERVER[‘HTTP_HOST‘].$_SERVER[‘REQUEST_URI‘];/* function*/sleep($time);file_get_contents($url);?>
The PHP script continues to execute after a period of time by accessing itself. It's like a relay race. This guarantees that each PHP script will not take too long to execute. It is not subject to time_out restrictions.
Because each time the PHP file is executed independently, this method avoids the time_out limitation. But it's best to add the control code to the top. Cofig.php so that the process can be terminated.
Four. Server timed Tasks
UNIX Platform
If you use a Unix system, you need to add a special line of code to the front of your PHP script so that it can be executed so that the system knows what program to run the script with. The first line of code added to the Unix system does not affect the script running under Windows, so you can also use this method to write a cross-platform scripting program.
1. Execute script in crontab using PHP
Just like calling a normal shell script in crontab (specific crontab usage), use PHP to invoke PHP scripts, and execute the myscript.php every hour as follows:
# crontab -e00 * * * * /usr/local/bin/php /home/john/myscript.php
/usr/local/bin/php is the path to the PHP program.
2. Execute script in crontab using URL
If your PHP script can be triggered by a URL, you can use Lynx or curl or wget to configure your crontab.
The following example uses the Lynx text browser to access the URL to execute PHP scripts every hour. The Lynx text browser opens the URL by default using dialog. However, like the following, we use the-dump option in the Lynx command line to convert the output of the URL to standard output.
00 * * * * lynx -dump http://www.sf.net/myscript.php
The following example uses CURL to access the URL to execute PHP scripts every 5 minutes. Curl defaults to show output in standard output. With the "curl-o" option, you can also dump the output of the script to a temporary file temp.txt.
*/5 * * * * /usr/bin/curl -o temp.txt http://www.sf.net/myscript.php
The following example uses the wget access URL to execute PHP scripts every 10 minutes. The-q option indicates quiet mode. "-O Temp.txt" indicates that the output is sent to a temporary file.
*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.sf.net/myscript.php
Five. Explanation of Ini_set function usage
PHP Ini_set is used to set the value of php.ini, which takes effect when the function executes, and the settings expire after the script is finished. You can modify the configuration without opening the php.ini file, which is convenient for virtual space.
function format:
string ini_set(string $varname, string $newvalue)
Not all parameters can be configured to view the list in the manual.
Common settings:
@ ini_set(‘memory_limit‘, ‘64M‘);
Menory_limit: Sets the maximum number of bytes of memory that a script can request, which helps write bad scripts that consume the available memory on the server. The @ symbol means no output error.
@ini_set(‘display_errors‘, 1);
Display_errors: Sets the category of the error message.
@ini_set(‘session.auto_start‘, 0);
Session.auto_start: Whether automatically open session processing, set to 1 o'clock, the program does not have to session_start () to manually open the session can also use the session,
If the parameter is 0 and the session is not manually opened, an error will be added.
@ini_set(‘session.cache_expire‘, 180);
Session.cache_expire: Specifies a time limit (minutes) for the session page to be saved in the client cache for 180 minutes. If Session.cache_limiter=nocache is set, the settings here are not valid.
@ini_set(‘session.use_cookies‘, 1);
Session.use_cookies: Whether to use a cookie to save the session ID on the client;
@ini_set(‘session.use_trans_sid‘, 0);
Session.use_trans_sid: Whether to display the SID (session ID) in the URL using plaintext,
The default is forbidden because it poses a security risk to your users:
Users may tell other people about URLs that contain valid SIDS through EMAIL/IRC/QQ/MSN and other means.
URLs that contain valid SIDS may be saved on the public computer.
Users may save URLs with immutable SIDs in their favorites or browsing history. URL-based session management is always more risky than cookie-based session management, so it should be disabled.
Several methods of implementing timed tasks in PHP