This article mainly shares with you several ways to implement the timing task in PHP, mainly in the form of text and code and explain to you, I hope to help you.
Simple, direct and reckless
Cons: After startup, you have no control unless you terminate the PHP host. Don’t do this unless you are a hacker.
1. How does PHP implement scheduled tasks on a regular basis? 1. Dugu Jiujian (4)_PHP video tutorial
Ii. Simple controllable type
Stop the program by changing config.php’s return 0. One possible way to do this is for the config.php file to interact with a particular form, using HTML pages to set variables for configuration
Disadvantages: occupying system resources, running for a long time, there will be some unexpected hidden trouble. Such as memory management issues.
Iii. Simple improvement
The PHP script sleeps for a while and then continues to execute by accessing itself. It’s like a relay race… This ensures that each PHP script does not take too long to execute, thus exempting it from time_out.
Because each loop of the PHP file is executed independently, this approach avoids the time_out constraint. But it is best to add the control code as above. Cofig. PHP, so that the process can be terminated.
Four. Server timing task
If you use a Unix system, you need to put a special line of code at 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 a Unix system does not affect how the script runs under Windows, so you can also write cross-platform scripts this way.
1. Use PHP to execute scripts in Crontab
Just like calling a normal shell script in Crontab (specific Crontab usage), use the PHP program to call the PHP script and execute myscript.php every hour as follows:
# crontab -e 00 * * * * /usr/local/bin/php /home/john/myscript.php
/usr/local/bin/php is the path to the PHP program.
2. Use URL to execute script in Crontab
If your PHP scripts can be triggered by a URL, you can configure your Crontab using Lynx or curl or Wget.
The following example USES the Lynx text browser to access the URL to execute the PHP script hourly. By default, the Lynx text browser opens the URL in dialog mode. However, as shown below, we use the -dump option on 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 a PHP script every 5 minutes. Curl displays the output in standard output by default. With the “curl -o” option, you can also dump the output of the script to a temporary file called temp.txt.
*/5 * * * * /usr/bin/curl -o temp.txt http://www.sf.net/myscript.php
The following example USES WGET to access the URL to execute a PHP script every 10 points. The -q option represents quiet mode. -o temp.txt” means the output will be sent to a temporary file.
*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.sf.net/myscript.php
Details 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 expires when the script ends. You can change the configuration without having to open the php.ini file, which is handy for virtual Spaces.
string ini_set(string $varname, string $newvalue)
Not all parameters can be configured, you can see the list in the manual.
@ ini_set('memory_limit', '64M');
Menory_limit: Sets the maximum number of bytes of memory a script can request, which makes it easy for poorly written scripts to consume the available memory on the server. The @ symbol means no error output.
Display_errors: Sets the category of error messages.
Session. Auto_start: Whether to automatically open session processing, when set to 1, the program does not use session_start() to manually open session or use session.
If the parameter is 0 and session is not opened manually, an error will be reported.
Session.cache_expire: Specifies that a deadline (minutes) in the client’s cache for the session page is, by default, 180 minutes. This setting is invalid if session.cache_limiter=nocache.
Session.use_trans_sid: Whether to display the SID (session ID) in the URL using plain code,
The default is disabled because it poses a security risk to your users:
Users may tell others the URL containing a valid SID via email/ IRC /QQ/MSN, etc.
A URL containing a valid SID may be saved on a public computer.
Users may save urls with fixed 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.
These are the details of several ways PHP implements timed tasks. Please follow the other articles in the IT learning garden!