Home > Backend Development > PHP Tutorial > Detailed explanation of several methods of executing tasks regularly in PHP

Detailed explanation of several methods of executing tasks regularly in PHP

WBOY
Release: 2016-07-25 08:58:54
Original
1171 people have browsed it
  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 .= " rn".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. }

Copy code

The two key functions: ignore_user_abort(true), the function of this function is that the following code will be executed regardless of whether the client closes the browser. set_time_limit(0), the function of this function is to cancel the execution time of the PHP file. If there is no such function, the default PHP execution time is 30 seconds, which means that after 30 seconds, the file will say goodbay.

In addition, usleep supports Windows operating system after PHP5.0.

When we are doing a PHP email sending problem, we often encounter this problem, that is, users subscribe to some information that needs to be sent to the user's mailbox regularly. I searched the Internet and found that there are not many articles like this. This article introduces A method implemented using PHP. The author has not been using PHP for a long time. Everyone is welcome to PP.

1. To achieve scheduled sending, the main problem to solve is timing. What kind of if should be added when writing a program? If a certain time = a certain time, then the page will be sent. However, to implement this process, the problem is that we have to execute this page before it can be sent. Therefore, the main problem to be solved is how to deliver the goods when the time comes The server executes this page regularly, which seems to be difficult to implement.

2. Open the php manual and find the command line mode of PHP. You can study it.

3. Solution: 1. On the Windows platform, you can associate the double-click attributes of cliphp.exe and .php files, or you can write a batch file to execute scripts with PHP. We put the written program in a directory as follows:

  1. E:web Timesend.php
  2. #!/usr/bin/php
  3. require_once("E:webincludesconfig.php");
  4. require_once("E:webincludesclassmail.class.php" );
  5. require_once("E:webincludesclasssmtp.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 = "Mr.";
  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 message has been sent to your registered email, and the current login has been logged out. \nPlease check the confirmation email and obtain a new login password. ";
  29. echo $feedback;
  30. }
  31. ?>
Copy the code

Write a bat file.

  1. @D:phpcliphp.exe E:webmail.php >d:phpclisendmail.log
  2. Pause
Copy code

Save it as: timesend.bat and place it in the @D:phpcliphp.exe directory

Add a scheduled task in window and that’s it!

5. Explanation. 1. The template I use to send emails is stored in the database. There are two other email sending classes that are not provided. If you want, you can contact me. 2. Use absolute paths when using requrie_once. 3. PHP's command line mode allows PHP scripts to run completely independently of the WEB server, so it can reduce the load on the server when sending a large number of emails. 4. Once again, I recommend that you read the PHP manual Chapter 23. PHP command line mode.

Actually, this is not a real way to automatically send emails, but in the WEB mode without desktop applications, this may be a better way~! , I want a system that truly realizes automatic sending of emails, in the service There is still a desktop application for support on the server side! So this automatic sending of emails is just a way to implement PHP programs to send emails!

  1. " . $mailtemplate[0]['mt_message']. "

  2. ";
  3. ignore_user_abort(); // Even if the Client is disconnected (such as closing the browser ), the PHP script can also continue to execute.
  4. set_time_limit(0); //The execution time is unlimited. The default execution time of PHP is 30 seconds. Through set_time_limit(0), the program can be executed without limit
  5. $interval=20 ; // Time interval unit is seconds
  6. $key_file="key.txt"; // Configuration file

  7. if (isset($_GET['s']))

  8. {
  9. if ($ _GET['s']=="0"){ // Stop working, but don't 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 it works
  31. ////////// workspace////////
  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 exit
  39. die("I am dying!");
  40. }
  41. sleep ($interval); // Wait for $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. ?>

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template