Implementation of scheduled tasks using the phpignore_user_abort () function

Source: Internet
Author: User
The phpignore_user_abort function indicates whether the script execution will be terminated if the ignore_user_abort setting is disconnected from the client in PHP4 and PHP5. This function returns the previous value (a Boolean value) set by user-abort ).

Php ignore_user_abort

Function description (in PHP 4 and in PHP 5)

Ignore_user_abort sets whether the script execution will be terminated when the client is disconnected.

This function returns the previous value (a Boolean value) set by user-abort ).

Function definition

Int ignore_user_abort ([string $ value])

Parameter description

Setting is optional. if it is set to true, the disconnection from the user is ignored. if it is set to false, the script stops running.

If this parameter is not set, the current setting is returned.

Prompt comment

Note: PHP does not detect whether the user is disconnected until it attempts to send messages to the client. simply use the echo statement to ensure that messages are sent. see flush () function.

Instance description

Example-1: an ignore_user_abort () example can be used with the set_time_limit () function and an endless loop to implement the task scheduling function.

  1.  
  2. // Ignore user aborts and allow the script
  3.  
  4. // To run forever
  5.  
  6. Ignore_user_abort (true );
  7.  
  8. Set_time_limit (0 );
  9.  
  10. Echo 'testing connection handling in php ';
  11.  
  12. // Run a pointless loop that sometime
  13.  
  14. // Hopefully will make us click away from
  15.  
  16. // Page or click the "Stop" button.
  17.  
  18. While (1)
  19.  
  20. {
  21.  
  22. // Did the connection fail?
  23.  
  24. If (connection_status ()! = CONNECTION_NORMAL)
  25.  
  26. {
  27.  
  28. Break;
  29.  
  30. }
  31.  
  32. // Sleep for 10 seconds
  33.  
  34. Sleep (10 );
  35.  
  36. }
  37.  
  38. // If this is reached, then the 'break'
  39.  
  40. // Was triggered from inside the while loop
  41.  
  42. // So here we can log, or perform any other tasks
  43.  
  44. // We need without actually being dependent on
  45.  
  46. // Browser.
  47.  
  48. ?>

Instance 1,

After the browser is closed, the program can continue running in the background. in this case, the ignore_user_abort () function is required;

  1. Ignore_user_abort (true); // Sets whether the script execution is interrupted when the client is disconnected.
  2.  
  3.  
  4. Set_time_limit (0 );
  5. $ File = '/tmp/ignore_user.txt ';
  6. If (! File_exists ($ file )){
  7. File_put_contents ($ file );
  8. }
  9. If (! $ Handle = fopen ($ file, 'A + B ')){
  10. Echo "not open file:". $ file;
  11. Exit;
  12. }
  13. $ I = 0;
  14. While ($ I <100 ){
  15. $ Time = date ("Y-m-d H: I: s", time ());
  16. Echo $ time. "\ n ";
  17. If (fwrite ($ handle, $ time. "\ n") === false ){
  18. Echo "not write file:". $ file;
  19. Exit;
  20. }
  21. Echo "write file time:". $ time. "\ n ";
  22. $ I ++;
  23. Sleep (2 );
  24. }
  25.  
  26. Fclose ($ handle );
With this code, you can still execute php scheduled tasks even after you close your browser.

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.