Php scheduled task detection user connection status

Source: Internet
Author: User
Php scheduled task detection user connection status implementation code. For more information, see

Php scheduled task detection user connection status implementation code. For more information, see

Scheduled tasks
The Code is as follows:
Ignore_user_abort (); // The user closes the browser and continues to execute
Set_time_limit (0); // no restriction on the running time
$ Interval = 3; // The interval between program loops in seconds

$ Link = mysql_connect ('localhost', 'username', 'passwd ');
Mysql_select_db ('test ');
Mysql_query ("set names 'utf8 '");

Do {
// Close the browser and stop start
Echo str_repeat ('', 4069); // php checks the user connection status only during output. the default value of output_buffering for some web servers is 4096 characters. to ensure that flush () is valid, set it to 4069.
Ob_flush ();
Flush ();
// The user closes the browser and stops the end

$ Query = "insert into 'test '. 'test _ demo' ('title', 'content') VALUES ('scheduled task ','". date ("Y-m-d H: I: s", time ()). "')";
Mysql_query ($ query); // use the write database verification program
Sleep ($ interval );
} While (true );

If no php is output, the user connection status cannot be detected. Even if you close the browser program, it will still run until the apache service is stopped or restarted.
The Code is as follows:
// Close the browser and stop start
Echo str_repeat ('', 4069); // php checks the user connection status only during output. the default value of output_buffering for some web servers is 4096 characters. to ensure that flush () is valid, set it to 4069.
Ob_flush ();
Flush ();
// The user closes the browser and stops the end

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.