PHP timed Run

Source: Internet
Author: User
Tags usleep
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.
Ignore_user_abort (TRUE);
Set_time_limit (0);

function Write_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);
}

function Do_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.

  • Related Article

    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.