Five methods for implementing scheduled tasks in PHP and five methods for implementing php

Source: Internet
Author: User

Five methods for implementing scheduled tasks in PHP and five methods for implementing php

Scheduled running tasks are important tasks for a website, such as regularly publishing documents and regularly clearing junk information. Most of today's websites are developed in PHP Dynamic Language, the implementation of PHP determines that it does not have Java and.. Net AppServer concept. http is a stateless protocol. PHP can only be triggered by users and called. After being called, it will automatically exit the memory without resident memory.

If you want PHP to implement scheduled tasks, you can use the following solutions:

1. Simple and result-free

<? Phpignore_user_abort (); // close the browser and run the PHP script. set_time_limit (0); // you can use set_time_limit (0) to enable unlimited execution of the program. ini_set ('memory _ limit ', '12m '); // set the memory limit $ interval = 60*30; // run do {// ToDo sleep ($ interval) every half hour; // wait for 5 minutes} while (true );

Disadvantages:Once started, it cannot be controlled unless you terminate the PHP host. Do not use this method unless you are a hacker.

2. Simple and controllable

Config. php <? Phpreturn 1;?> Cron. php ignore_user_abort (); // close the browser and run the PHP script. set_time_limit (0); // use set_time_limit (0) to allow unlimited execution of the program $ interval = 60*30; // run do {$ run = include 'config. php'; if (! $ Run) die ('process abort '); // ToDo sleep ($ interval); // wait 5 minutes} while (true );

By changing the config. phpreturn 0 To stop the program. A feasible method is to interact with a special form in the config. php file and configure some variables through the HTML page.

Disadvantages:Occupying system resources and running for a long time may cause unexpected risks. For example, memory management problems.

3. Simple and improved

<?php$time=15;$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];/*  function*/sleep($time);file_get_contents($url);?>

The php script sleep continues execution after a period of time by accessing itself. It is like a race. This ensures that the execution time of each PHP script is not too long.time_out.

This method avoidstime_outBut it is better to add control code. cofig. php like above to terminate the process.

4. Server scheduled tasks

Unix platform

If you use a Unix system, you need to add a special line of code at the beginning of your PHP script so that it can be executed, in this way, the system will know what program to use to run the script. The first line of code added to the Unix system does not affect the running of the script in Windows. Therefore, you can use this method to compile a cross-platform scripting program.

1. Run the script in Crontab using PHP.

Just like calling a common shell script in Crontab (specific Crontab usage), you can use a PHP program to call the PHP script and execute myscript. php every hour as follows:

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

/usr/local/bin/phpThe path of the PHP program.

2. Use the URL in Crontab to execute the script

If your PHP script can be triggered through a URL, you can use lynx, curl, or wget to configure your Crontab.

The following example uses the Lynx text browser to access the URL and execute the PHP script hourly. By default, the Lynx text browser opens a URL in dialog mode. However, as shown below, we use the-dump option in the lynx command line to convert URL output to standard output.

00 * * * * lynx -dump http://www.sf.net/myscript.php


The following example uses CURL to access the URL and execute the PHP script every 5 minutes. Curl displays the output in standard output by default. With "curl-o", you can also store your own output to the temporary file temp.txt.

*/5 * * * * /usr/bin/curl -o temp.txt http://www.sf.net/myscript.php

The following example uses the wget url to execute the PHP script every 10 minutes. -Q indicates quiet mode. "-O temp.txt" indicates that the output will be sent to a temporary file.

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

5. Explanation of ini_set function usage

PHP ini_setIt is used to set the php. ini value, which takes effect when the function is executed. After the script is completed, the setting becomes invalid. 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. You can view the list in the manual.

Common settings:

@ ini_set('memory_limit', '64M');

Menory_limit:Set the maximum number of memory bytes that a script can apply for. This helps the poorly written script to consume the available memory on the server. @ Symbol indicates no output error.

@ini_set('display_errors', 1);

Display_errors:Set the category of the error message.

@ini_set('session.auto_start', 0);

Session. auto_start:Whether to enable session processing automatically. If this parameter is set to 1, you can enable session manually without session_start () in the program,

If the parameter is 0 and the session is not enabled manually, an error is returned.

@ini_set('session.cache_expire', 180);

Session. cache_expire:Specify the time limit (in minutes) for the session page in the Client cache. The default value is 180 minutes. If session. cache_limiter = nocache is set, no effect is set here.

@ini_set('session.use_cookies', 1);

Session. use_cookies:Whether to use cookies to save the session ID on the client;

@ini_set('session.use_trans_sid', 0);

Session. use_trans_sid:Whether to use an explicit Code to display SID (session ID) in the URL ),

It is disabled by default because it brings security risks to your users:

The user may send a URL containing a valid sid to another user by email, irc, QQ, or MSN.

URLs that contain valid sid may be stored on the public computer.

Users may save URLs with fixed SID 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 scheduled tasks are very interesting. The above are some solutions provided in this Article. You can also develop your own solutions through the ideas in this article. Hope to help everyone who needs it.

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.