Reprinted; detailed explanation of PHP implementation of scheduled tasks; detailed explanation of php implementation
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
12345678910 |
<?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 ini_set ( 'memory_limit' , '512M' ); // Set the memory limit $interval =60*30; // Run every 30 minutes do { //ToDo sleep( $interval ); // 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
1234567891011121314151617181920 |
config.php <?php return 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 every 30 minutes do { $run = include 'config.php' ; if (! $run ) die ( 'process abort' ); //ToDo sleep( $interval ); // Wait for 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
123456789 |
<?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_out
But 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:
12 |
# crontab -e 00 * * * * /usr/local/bin/php /home/john/myscript .php |
/usr/local/bin/php
The 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.
1 |
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.
1 |
* /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.
1 |
* /10 * * * * /usr/bin/wget -q -O temp.txt http: //www .sf.net /myscript .php |
5. Explanation of ini_set function usage
PHP ini_set
It 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:
1 |
string ini_set (string $varname , string $newvalue ) |
Not all parameters can be configured. You can view the list in the manual.
Common settings:
1 |
@ 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.
1 |
@ ini_set ( 'display_errors' , 1); |
Display_errors:Set the category of the error message.
1 |
@ 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.
1 |
@ 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.
1 |
@ ini_set ( 'session.use_cookies' , 1); |
Session. use_cookies:Whether to use cookies to save the session ID on the client;
1 |
@ 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.