PHP regular run

Source: Internet
Author: User
Tags usleep
PHP regularly runs the following code. a test.txt file can be generated under the current folder, and a timestamp can be written to it 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 ();
}

Two key functions:

Ignore_user_abort (true). The function is used to execute the following code regardless of whether the browser is closed on the client.

Set_time_limit (0). The function is used to cancel the execution time of the php file. if this function is not available, the default php execution time is 30 seconds, that is, 30 seconds later, this file says goodbay.

In addition, usleep supports the windows operating system after PHP5.0.

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.