How PHP performs tasks on a timed basis

Source: Internet
Author: User
Tags php server usleep

The implementation of PHP has decided that it does not have the concept of Java and. NET AppServer, and the HTTP protocol is a stateless protocol, PHP can only be triggered by the user, is called, the call will automatically exit memory, no resident memory, there is no way to accurate timing processing then, If you need to perform certain tasks in PHP on a timed basis, there are two ways to do this:

Linux under Crontab, Windows under the scheduled task to find a frequently called Web page, with a detection code set_time_limit (0); Ignore_user_abort (true); The first method of dead loop is the most common one, If the PHP server does not have permissions to Crontab, you can also find a machine on their own regular crontab to request the server the second method, the Forum on a regular basis to clean up the new post is basically so, if the sentiment is not prosperous, you can consider to other hot spots in the forum to send a picture, It's good to call your own PHP to implement a trigger. Discuz! is the judgment after 00:00, the first performer to execute. The third kind of comparison is not reliable, the Apache restart will have to revisit, fastcgi will be better.

The following code, under the current folder, can generate a test.txt and write a timestamp every 20 seconds, regardless of whether the client closes the browser.

<?php
Ignore_user_abort (true);Set_time_limit(0);functionWrite_txt ()
{if(!file_exists("Test.txt")) {$fp=fopen("Test.txt", "WB");fclose($fp);}$str=file_get_contents(' Test.txt ');$str. = "\ r \ n".Date("H:i:s");$fp=fopen("Test.txt", "WB");fwrite($fp,$str);fclose($fp);}functionDo_cron () {Usleep(20000000); Write_txt ();} while(1) {Do_cron ();}

The two key functions:

Ignore_user_abort (TRUE), the function is that the following code will be executed regardless of whether the client closes the browser or not.

Set_time_limit (0), the function is to cancel the php file execution time, if there is no such function, the default PHP execution time is 30 seconds, that is, 30 seconds later, the file is say Goodbay.

The Usleep also supports the Windows operating system after PHP5.0.

We are doing a PHP mail sending problem is often met with such a problem, is that the user to subscribe to some of the information needs to be sent to the user's mailbox regularly. To search the Internet, found in such articles are not many, this article introduced a method of implementation of PHP, the author of the time with PHP is not long, welcome everyone pp.

First, to achieve timed delivery, the main problem is timing.

We need to add something when we write the program if a time = a time is sent, but to achieve this process, the problem is that we have to execute this page to send. So the main problem is how to get the server to execute this page on time, so it seems to be difficult to implement.

Second, I opened the PHP manual, found the PHP command line mode This chapter, found to solve the problem, it is recommended that if you want to use this method, first look at this chapter.

Third, the solution:

1. Under the Windows platform you can associate the double-click properties of the Cli\php.exe and. php files, or you can write a batch file to execute the script in PHP. We put the well-written program in a directory like E:\web\mail.php

Then write a Windows batch file with the contents as follows.

@d:\php\cli\php.exe E:\web\mail.php >d:\php\cli\sendmail.log

Pause

That D:\php\cli\php.exe is the directory where my PHP installation files are located. Php.exe is the Windows PHP command-line mode of the program.

OK, let's save this file for Mail.bat then add a task to the scheduled task in Windows and let the operating system run the batch file at some time.

2. If you are using 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 run the script. The first line of code added to the Unix system does not affect the script running under Windows, so you can also use this method to write a cross-platform scripting program.

Ignore_user_abort (); // turn off the browser and the PHP script will continue to execute. Set_time_limit (0); // set_time_limit (0) allows the program to be implemented without restrictions $interval=60*30; // running every half hour  Do {    // Here is the    code you want to execute    sleep($interval);   wait 5 minutes }while (true);

How PHP performs tasks on a timed basis

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.