Several methods for php scheduled task execution

Source: Internet
Author: User
Tags usleep
Several methods for php scheduled task execution

  1. Ignore_user_abort (true );

  2. Set_time_limit (0 );

  3. Function write_txt (){

  4. If (! File_exists(”test.txt ")){
  5. $ Fp = fopen(‑test.txt "," wb ");
  6. Fclose ($ fp );
  7. }
  8. $ Str = file_get_contents('test.txt ');
  9. $ Str. = "\ r \ n". date ("H: I: s ");
  10. $ Fp = fopen(‑test.txt "," wb ");
  11. Fwrite ($ fp, $ str );
  12. Fclose ($ fp );
  13. }

  14. Function do_cron (){

  15. Usleep (20000000 );
  16. Write_txt ();
  17. }

  18. While (1 ){

  19. Do_cron ();
  20. }

Two key functions: ignore_user_abort (true). The function is used to execute the following code regardless of whether the client closes the browser. 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.

We often encounter this problem when sending php Mail, that is, users need to regularly send some materials to their mailboxes for subscription. I searched the internet and found that there are not many such articles. This article introduces a method implemented using PHP. I have not used PHP for a long time. welcome to PP.

1. scheduled transmission is the main solution to the problem. When writing a program, you need to add a message if at a certain time = a certain time. however, to implement this process, the problem is that we need to execute this page before sending the message. Therefore, the main problem to be solved is how to make the server regularly execute this page at that time, which seems difficult to implement.

2. open the php manual and find the PHP command line mode.

3. Solution: 1. on Windows, you can set cli \ php.exe and. the double-click attribute of the php file is associated. you can also compile a batch file to execute the script in PHP. We put the program in a directory, as shown in figure

  1. E: \ web \ Timesend. php
  2. #! /Usr/bin/php
  3. Require_once ("E: \ web \ shortdes \ config. php ");
  4. Require_once ("E: \ web \ shortdes \ class \ mail. class. php ");
  5. Require_once ("E: \ web \ shortdes \ class \ smtp. class. php ");
  6. // + ------ +
  7. // Database configuration
  8. $ Dbhost = "localhost ";
  9. $ Dbport = "3306 ";
  10. $ Dbname = "";
  11. $ Dbuser = "";
  12. $ Dbpawd = "";
  13. // + --------- +
  14. // Database connection object
  15. $ Db = new dbLink ($ dbhost, $ dbport, $ dbuser, $ dbpawd, $ dbname );
  16. $ Query = "SELECT * FROM wl_mailtemplate WHERE mt_name = 'userupdate '";
  17. $ Mailtemplate = $ db-> dbQuery ($ query );
  18. $ Username = 'sdfsdfdsd ';
  19. $ Sex = "Sir ";
  20. $ Accounts = "sdfasdfasdfsad ";
  21. $ Password = "sdfsadfsdasdasddssfds ";
  22. $ Message ="
  23. $ Message = addslashes ($ message );
  24. Eval_r ("\ $ message = \" $ message \";");
  25. $ Mail = new SendMail ('wfits @ jbxue.com ', $ mailtemplate [0] ['Mt _ subject'], nl2br ($ message ));
  26. If ($ mail-> Send ())
  27. {
  28. $ Feedback = "The modification confirmation information has been sent to your registration Email and the current logon has been canceled. \ N check the confirmation letter and obtain a new logon password. ";
  29. Echo $ feedback;
  30. }
  31. ?>

Write a bat file.

  1. @ D: \ php \ cli \ php.exe E: \ web \ mail. php> d: \ php \ cli \ sendmail. log
  2. Pause

Save timesend. bat in the @ D: \ php \ cli \ php.exe directory.

Add a scheduled task to the window!

5. description. 1. I use a template to store emails in the database, and two other mail sending classes are not provided. if you want to, contact me. 2. use the absolute path when using requrie_once. 3. the PHP command line mode allows the PHP script to run independently of the WEB server. Therefore, it can reduce the burden on the server if a large number of emails are sent. 4. once again, we suggest you look at the PHP command line mode in chapter 23rd of the PHP Manual.

In fact, this is not a real method to implement automatic mail sending, but in the WEB mode of the desktop application, this may be a better way ~!, I think there is still a desktop application on the server side to support the system that implements automatic mail sending! Therefore, the implementation of automatic mail sending is only one way to implement the PHP program to send mail!

  1. ". $ Mailtemplate [0] ['Mt _ message']."

  2. ";
  3. Ignore_user_abort (); // The PHP script can be executed even if the Client is disconnected (such as the browser is disabled.
  4. Set_time_limit (0); // the execution time is unlimited. the default php execution time is 30 seconds. you can use set_time_limit (0) to limit the execution of the program.
  5. $ Interval = 20; // time interval in seconds
  6. $ Key_file = "key.txt"; // configuration file

  7. If (isset ($ _ GET ['s '])

  8. {
  9. If ($ _ GET ['s '] = "0") {// stop the job, but do not exit
  10. $ S = "false ";
  11. Echo "Function is off ";
  12. }
  13. Elseif ($ _ GET ['s '] = "1") {// Work
  14. $ S = "true ";
  15. Echo "Function is on ";
  16. }
  17. Elseif ($ _ GET ['s '] = "2") {// exit
  18. $ S = "die ";
  19. Echo "Function exited ";
  20. }
  21. Else
  22. Die ("Err 0: stop working 1: working 2: exit ");
  23. $ String =" ";
  24. Write_inc ($ key_file, $ string, true );
  25. Exit ();
  26. }

  27. If (file_exists ($ key_file )){

  28. Do {
  29. $ Mkey = include $ key_file;
  30. If ($ mkey = "true") {// if the job
  31. /// // Work interval ////////
  32. $ Showtime = date ("Y-m-d H: I: s ");
  33. $ Fp = fopen('func.txt ', 'A ');
  34. Fwrite ($ fp, $ showtime. "\ n ");
  35. Fclose ($ fp );
  36. ////////////////
  37. }
  38. Elseif ($ mkey = "die") {// if you exit
  39. Die ("I am dying! ");
  40. }
  41. Sleep ($ interval); // Wait $ interval minutes
  42. } While (true );
  43. }
  44. Else
  45. Die ($ key_file. "doesn't exist! ");
  46. // By bbs.it-home.org

  47. Function write_inc ($ path, $ strings, $ type = false)

  48. {
  49. $ Path = dirname (_ FILE _). "/". $ path;
  50. If ($ type = false)
  51. File_put_contents ($ path, $ strings, FILE_APPEND );
  52. Else
  53. File_put_contents ($ path, $ strings );
  54. }
  55. ?>

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.