-
-
ignore_user_abort(true); - set_time_limit(0);
function write_txt(){
- if(!file_exists("test. txt")){
- $fp = fopen("test.txt","wb");
- fclose($fp);
- }
- $str = file_get_contents('test.txt');
- $str .= " rn".date("H:i:s");
- $fp = fopen("test.txt","wb");
- fwrite($fp,$str);
- fclose($fp);
- }
function do_cron(){
- usleep(20000000);
- write_txt();
- }
while(1){
- do_cron();
- }
-
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:
-
- E:web Timesend.php
- #!/usr/bin/php
- require_once("E:webincludesconfig.php");
- require_once("E:webincludesclassmail.class.php" );
- require_once("E:webincludesclasssmtp.class.php");
- // +------+
- //Database configuration
- $dbhost = "localhost";
- $dbport = "3306";
- $ dbname = "";
- $dbuser = "";
- $dbpawd = "";
- // +---------+
- //Database connection object
- $db = new dbLink($dbhost,$ dbport,$dbuser,$dbpawd,$dbname);
- $query = "SELECT * FROM wl_mailtemplate WHERE mt_name = 'UserUpdate'";
- $mailtemplate =$db->dbQuery($query);
- $username = 'sdfsdfdsd ';
- $sex = "Mr.";
- $accounts = "sdfasdfasdfsad";
- $password = "sdfsadfsdasdasddssfds";
- $message = "
- $message = addslashes($message);
- eval_r("$message = "$ message";");
- $mail = new SendMail('wfits@jbxue.com', $mailtemplate[0]['mt_subject'], nl2br($message));
- if ($mail->Send() )
- {
- $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. ";
- echo $feedback;
- }
- ?>
-
Copy the code
Write a bat file.
-
- @D:phpcliphp.exe E:webmail.php >d:phpclisendmail.log
- 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!
-
-
" . $mailtemplate[0]['mt_message']. "
- ";
-
- ignore_user_abort(); // Even if the Client is disconnected (such as closing the browser ), the PHP script can also continue to execute.
- 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
- $interval=20 ; // Time interval unit is seconds
- $key_file="key.txt"; // Configuration file
if (isset($_GET['s']))
- {
- if ($ _GET['s']=="0"){ // Stop working, but don't exit
- $s="false";
- echo "Function is off";
- }
- elseif ($_GET['s']= ="1"){ // Work
- $s="true";
- echo "Function is on";
- }
- elseif ($_GET['s']=="2"){ // Exit
- $s ="die";
- echo "Function exited";
- }
- else
- die("Err 0:stop working 1:working 2:exit");
-
- $string = "";
- write_inc($key_file,$string,true);
- exit();
- }
if(file_exists($key_file)){
- do {
- $mkey = include $key_file;
- if ($mkey=="true"){ // If it works
- ////////// workspace////////
- $showtime= date("Y-m-d H:i:s");
- $fp = fopen('func.txt','a');
- fwrite($fp,$showtime."n");
- fclose($fp);
- ////////////////
- }
- elseif ($mkey=="die"){ // If exit
- die("I am dying!");
- }
- sleep ($interval); // Wait for $interval minutes
- }while(true);
- }
- else
- die($key_file." doesn't exist !");
- //by bbs.it-home.org p>
function write_inc($path,$strings,$type=false)
- {
- $path=dirname(__FILE__)."/".$path;
- if ($type==false)
- file_put_contents ($path,$strings,FILE_APPEND);
- else
- file_put_contents($path,$strings);
- }
- ?>
-
Copy code
|