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 time limit in php

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to achieve time limit in php". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to achieve time limit in php"!

Php to achieve time-limited methods: 1, through the "set_time_limit (0)" to allow the program to execute without limit; 2, through ini_set to set the memory limit; 3, to set every half an hour to run.

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

How does php achieve a time limit?

Five methods of realizing scheduled tasks with PHP

These days you need to use PHP to write a server application that crawls web pages regularly. Search the Internet for solutions and find several solutions, which are summarized as follows.

Running tasks regularly is an important task for a website, such as regularly publishing documents, regularly cleaning up spam, etc. Most websites are developed in PHP dynamic language, but the implementation of PHP determines that it does not have the concept of AppServer such as Java and .net, while http protocol is a stateless protocol. PHP can only be triggered by the user, is called, and automatically exits memory after it is called. There is no resident memory.

If PHP is required to implement scheduled tasks, there are several solutions:

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

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

At this point, I believe you have a deeper understanding of "how to achieve time limit in php". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report