Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize scheduled tasks in PHP

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve timing tasks in PHP. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

one。 Simple and direct regardless of the consequences

Cron.php ignore_user_abort (); / / close the browser, and the PHP script can also continue to execute .set _ time_limit (0); / / the program can be executed indefinitely via set_time_limit (0). $interval=60*30;// runs do {$run = include 'config.php'; if (! $run) die (' process abort') every half hour; / / ToDo sleep ($interval) / / wait 5 minutes} while (true)

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

Disadvantages: take up system resources, run for a long time, there will be some unexpected hidden dangers. For example, there are problems with memory management.

three。 Simple improved type

The php script sleep continues to execute after a period of time by accessing itself. It's like a relay race.. This ensures that each PHP script will not take too long to execute. It is not subject to the restrictions of time_out.

Because each loop php file is executed independently, this method avoids the limitation of time_out. But it is better to add the control code as above. Cofig.php so that the process can be terminated.

four。 Server scheduled task

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 use to run the script. The first line of code added to the Unix system does not affect how the script runs under Windows, so you can also use this method to write cross-platform scripts.

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-E00 * / usr/local/bin/php / home/john/myscript.ph

/ usr/local/bin/php is the path of the PHP program.

2. Use URL to execute scripts in Crontab

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

The following example uses a Lynx text browser to access URL to execute PHP scripts on an hourly basis. Lynx text browsers open URL by default in dialog mode. However, as shown below, we use the-dump option on the lynx command line to convert the output of URL to standard output.

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

The following example uses CURL to access URL to execute PHP scripts every 5 points. Curl displays output in standard output by default. Using the "curl-o" option, you can also dump the output of the script to the temporary file temp.txt.

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

The following example uses WGET to access URL to execute PHP scripts every 10 points. The-Q option 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

five。 Detailed explanation of the usage of ini_set function

PHP ini_set is used to set the value of php.ini, which takes effect when the function is executed, and the setting becomes invalid after the end of the script. You can modify the configuration without opening the php.ini file, which is convenient for virtual spaces.

Function format:

String ini_set (string $varname, string $newvalue)

Not all parameters can be configured, you can check the list in the manual.

Common settings:

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

Menory_limit: set the maximum number of bytes of memory that a script can apply for, which helps poorly written scripts consume available memory on the server. The @ symbol indicates that errors are not output.

@ ini_set ('display_errors', 1)

Display_errors: sets the category of the error message.

@ ini_set ('session.auto_start', 0)

Session.auto_start: whether to enable session processing automatically. If set to 1, you can also use session without using session_start () to manually open session in the program.

If the parameter is 0 and session is not manually turned on, an error will be reported.

@ ini_set ('session.cache_expire', 180)

Session.cache_expire: specifies that the limited duration (minutes) of the session page in the client cache defaults to 180 minutes. If session.cache_limiter=nocache is set, the setting here has no effect.

@ ini_set ('session.use_cookies', 1)

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

@ ini_set ('session.use_trans_sid', 0)

Session.use_trans_sid: whether to use plain code to display SID in URL (session ID)

It is forbidden by default because it poses a security risk to your users:

Users may tell others about the URL that contains a valid sid through email/irc/QQ/MSN and so on.

URL that contains a valid sid may be saved on a public computer.

Users may save URL with a 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.

On how to achieve timed tasks in PHP to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report