PHP functions to be executed after the browser is closed

Source: Internet
Author: User
You can use the ignore_user_abort function to schedule tasks in PHP. For more information, see.

You can use the ignore_user_abort function to schedule tasks in PHP. For more information, see.

Forget this function:
Function Name: ignore_user_abort
Whether or not the PHP program continues to be executed on the Hong Kong server after the function is configured or the connection is interrupted on the user side. The default value is to stop running after the connection is interrupted. In the PHP configuration file (php3.ini/php. ini), The ignore_user_abort option is the configuration. This function is available only after PHP 3.0.7.
Official notes:


Usage:

The Code is as follows:


Ignore_user_abort (true); // The PHP script can be executed even if the Client is disconnected (such as the browser is disabled.


In this way, you can achieve the task scheduling effect, but you still need to access the program from the client.
For example, when generating static pages and collecting data, there is no record-filing space, and Hong Kong servers are rented, so there is no need to wait. Turn off the browser.
Example:

The Code is as follows:


// Test
Set_time_limit (0 );
Ignore_user_abort (true );
$ I = 0;
While ($ I ++ <200 ){
File_put_contents ($ I. '. php', $ I );
Sleep (3 );
}


Use the ignore_user_abort function to implement php Task Scheduling

The Code is as follows:


Ignore_user_abort (true );
Set_time_limit (0 );
While (1 ){
$ Fp = fopen('time_task.txt ', "a + ");
$ Str = date ("Y-m-d h: I: s"). "\ n \ r ";
Fwrite ($ fp, $ str );
Fclose ($ fp );
Sleep (5); // run once every 30 minutes
}
?>

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.