PHPignore_user_abort, connection_status, and register_shutdown_function usage

Source: Internet
Author: User
Tags php website
PHPignore_user_abort and connection_status and register_shutdown_function use ignore_user_abort () to implement PHP scheduled task syntax: intignore_user_abort (int [setting]); return value: integer function type: PHP system function description 0-

PHP ignore_user_abort, connection_status, and register_shutdown_function usage
Use ignore_user_abort () to schedule tasks in PHP
Syntax: int ignore_user_abort (int [setting]);
Return value: integer
Function types: PHP system functions
Description
0-NORMAL (NORMAL) 1-ABORTED (abnormal exit) 2-TIMEOUT (TIMEOUT)
Whether the PHP program continues to run after the function is configured or the connection is interrupted. 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.
Ignore_user_abort. This function can help us implement scheduled tasks like cron in linux. you can also execute this function after turning off the browser.
First modify your php. ini, remove the comment "; ignore_user_abort = On", and restart Apache or IIS.
Use sleep (300) + endless loops to implement scheduled tasks. there is no output in this program and it is easy to interrupt the output.
Disadvantage: to stop a program, you must restart the server, which occupies a large amount of memory !!
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 ";
Fwrite ($ fp, $ str );
Fclose ($ fp );
Sleep (1800); // executed once every 30 minutes
}
?>
PHP code
Ignore_user_abort (true); // enable the function
Set_time_limit (0); // The script is executed infinitely.
$ Interval = 60*2; // execute every two minutes
Do {
// The execution content starts.
File_get_contents ("http://www.liujingze.com/"); // The process to be executed
// The execution content ends.
Sleep ($ interval); // run at the specified interval
} While (true );
?>
PHP scheduled task instance code
Ignore_user_abort ();
Set_time_limit (0 );
$ Interval_second = 15;
Do {
@ $ Fp = fopen ("leapsoulcn.txt", "");
If (! $ Fp)
{
Echo "system error ";
Exit ();
}
Else
{
$ FileData = "domain". "". "www.leapsoul.cn "."";
$ FileData = $ fileData. "description". "". "PHP website development tutorial network for PHP beginners. "."";
$ FileData = $ fileData. "title". "". "this tutorial describes how to use the ignore_user_abort function to schedule tasks in PHP. ";
Fwrite ($ fp, $ fileData );
Fclose ($ fp );
}
Sleep ($ interval_second );
} While (true );
?>

Connection_status
Obtain the connection status.
Syntax: int connection_status (void );
Return value: integer
Function type: Network System
Description
This function returns the connection status. No parameter is required for use.
 
Register_shutdown_function
Define the function to be executed after the PHP program is executed.
Syntax: int register_shutdown_function (string func );
Return value: integer
Function types: PHP system functions
Description
This function defines to the system the function to be executed after the PHP program (Script) is executed. When executing the specified function, it is difficult to debug because the returned value cannot be seen.

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.