Five ways to implement timed tasks in PHP

Source: Internet
Author: User
Tags session id sleep function

Scheduled operation of the task for a site, is a more important task, such as the timing of the release of documents, scheduled cleanup spam, and so on, most of the current Web site is the use of PHP dynamic language development, and the implementation of PHP has decided that it does not have the concept of Java and. NET AppServer, and the HTTP protocol is a stateless protocol, PHP can only be triggered by the user, is called, the call will automatically exit memory, no resident memory.

If you do not want PHP to implement scheduled tasks, you can have the following solutions:

I. Simple direct disregard of the consequences type

?
12345678910 <?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

?
1234567891011121314151617181920 config.php<?phpreturn 1;?>cron.php  ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行.set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去$interval=60*30;// 每隔半小时运行do{  $run = include ‘config.php‘;  if(!$run) die(‘process abort‘);    //ToDo  sleep($interval);// 等待5分钟}while(true);

Stop the program by changing the config.php return 0  . One possible way is to interact with the config.php file and a special form, set some variables on the HTML page to configure

Disadvantages: Accounting for system resources, long-running, there will be some unexpected hidden dangers. For example, memory management problems.

Three. Simple and improved type

?
123456789 <?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. Will not be time_out limited.

Because each time the PHP file is executed independently, this method avoids time_out the 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:

?
12 # crontab -e00 * * * * /usr/local/bin/php/home/john/myscript.php

/usr/local/bin/phpThe 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.

?
1 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.

?
1 */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.

?
1 */10* * * * /usr/bin/wget-q -O temp.txt http://www.sf.net/myscript.php

Five. Explanation of Ini_set function usage

PHP ini_setUsed to set the value of the php.ini, which takes effect when the function executes, and when the script finishes, the settings expire. You can modify the configuration without opening the php.ini file, which is convenient for virtual space.

function format:

?
1 string ini_set(string $varname, string $newvalue)

Not all parameters can be configured to view the list in the manual.

Common settings:

?
1 @ 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.

?
1 @ini_set(‘display_errors‘, 1);

display_errors: Sets the category of the error message.

?
1 @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.

?
1 @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.

?
1 @ini_set(‘session.use_cookies‘, 1);

session.use_cookies: whether to use a cookie to save the session ID on the client;

?
1 @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.

PHP Timer task is a very interesting thing, the above is the solution provided by this article, you can also through the idea of this article, develop a solution of their own. Hope to help everyone in need.

If you have questions about this article, please submit to the Exchange community, the vast number of enthusiastic netizens will answer for you!! Click to enter the community

Articles you may be interested in:
    • Implementation of PHP implementation of timed tasks
    • PHP 3 ways to perform tasks in a timed manner
    • How to implement Timer tasks with pure PHP (timer)
    • PHP timed implementation of Task implementation method (timer)
    • PHP scheduled execution of task settings detailed
    • Ignore_user_abort function Implementation method of PHP planning task
    • How PHP Implements timed tasks
    • Using the Sleep function in PHP to share timed task instances
    • PHP version of Cron Timer task Executor usage instance
    • How to perform PHP tasks offline
From:http://www.jb51.net/article/89186.htm

Five ways to implement timed tasks in PHP

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.