Several ways to implement timed tasks in PHP

Source: Internet
Author: User
This article is mainly to share with you several kinds of PHP implementation of the timing task of the way, mainly in text and code of the way and we explain, hope to help everyone.

I. Simple direct disregard of the consequences type

    1. <?php  Ignore_user_abort ();//switch off the browser, PHP script can continue to execute.  Set_time_limit (0);//Set_time_limit (0) allows the program to execute indefinitely  ini_set (' memory_limit ', ' 512M ');//Set memory limit  $interval =60*30;//run  do{      //todo       sleep ($interval) every half hour;//wait 5 minutes  } 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

    1. <?php  return 1;  ? >

cron.php

    1. Ignore_user_abort ();//switch off the browser, PHP script can continue to execute.  Set_time_limit (0);//Set_time_limit (0) allows the program to execute indefinitely  $interval =60*30;//runs do{every half hour      $run = include ' Config.php ';      if (! $run) die (' Process abort ');            ToDo      Sleep ($interval);//wait 5 minutes  } 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

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

XX * * * * 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:

    1. Users may tell other people about URLs that contain valid SIDS through EMAIL/IRC/QQ/MSN and other means.

    2. URLs that contain valid SIDS may be saved on the public computer.

    3. 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.

Related recommendations:

PHP combined with Linux cron command to implement timed task instances

Linux timed Task crontab command detailed

php to perform timed tasks

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.