PHP implementation has decided that it does not have Java and. NET this appserver concept, and the HTTP protocol is a stateless protocol, PHP can only be triggered by the user, is called, the call will automatically exit memory, no resident memory, there is no way to accurately handle the timing
Then, if you need to perform certain tasks in PHP at timed intervals, there are two ways to do this:
Linux under Crontab, Windows under Scheduled Tasks
Find a page that is frequently called, plus a detection code
Set_time_limit (0);
Ignore_user_abort (TRUE);
Dead loop
The first method is the most common, if the PHP server does not have permissions to Crontab, you can also find a machine on their own regular crontab to request the server
The second method, the forum regularly clean up the new post is basically so realized, if the sentiment is not prosperous, you can consider going to other hot spots in the forum to send a picture, call their own PHP to achieve a trigger just fine. Discuz! is the judgment after 00:00, the first performer to execute.
The third kind of comparison is not reliable, the Apache restart will have to revisit, fastcgi will be better.
The following code, under the current folder, can generate a test.txt and write a timestamp every 20 seconds, regardless of whether the client closes the browser.
<?php
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. = "\ r \ n". 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 ();
}
The two key functions:
Ignore_user_abort (TRUE), the function is that the following code will be executed regardless of whether the client closes the browser or not.
Set_time_limit (0), the function is to cancel the php file execution time, if there is no such function, the default PHP execution time is 30 seconds, that is, 30 seconds later, the file is say Goodbay.
The Usleep also supports the Windows operating system after PHP5.0.
We are doing a PHP mail sending problem is often met with such a problem, is that the user to subscribe to some of the information needs to be sent to the user's mailbox regularly. To search the Internet, found in such articles are not many, this article introduced a method of implementation of PHP, the author of the time with PHP is not long, welcome everyone pp.
First, to achieve timed delivery, the main problem is timing.
We need to add something when we write the program if a time = a time is sent, but to achieve this process, the problem is that we have to execute this page to send. So the main problem is how to get the server to execute this page on time, so it seems to be difficult to implement.
Second, I opened the PHP manual, found the PHP command line mode This chapter, found to solve the problem, it is recommended that if you want to use this method, first look at this chapter.
Third, the solution:
1. Under the Windows platform you can associate the double-click properties of the Cli\php.exe and. php files, or you can write a batch file to execute the script in PHP. We put the well-written program in a directory like E:\web\mail.php
Then write a Windows batch file with the contents as follows.
@d:\php\cli\php.exe E:\web\mail.php >d:\php\cli\sendmail.log
Pause
That D:\php\cli\php.exe is the directory where my PHP installation files are located. Php.exe is the Windows PHP command-line mode of the program.
OK, let's save this file for Mail.bat then add a task to the scheduled task in Windows and let the operating system run the batch file at some time.
2. If you are using a Unix system, you need to add a special line of code to the front of your PHP script so that it can be executed so that the system knows what program to run the script. The first line of code added to the Unix system does not affect the script running under Windows, so you can also use this method to write a cross-platform scripting program. The following is an example of a simple PHP command-line program.
Iv. Examples:
E:\web \timesend.php
#!/usr/bin/php
<?php
Require_once ("E:\web\includes\config.php");
Require_once ("E:\web\includes\class\mail.class.php");
Require_once ("E:\web\includes\class\smtp.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 = "Sir";
$accounts = "Sdfasdfasdfsad";
$password = "Sdfsadfsdasdasddssfds";
$message = "
$message = Addslashes ($message);
Eval_r ("\ $message = \" $message \ ";");
$mail = new SendMail (' [email protected] ', $mailtemplate [0][' Mt_subject '], NL2BR ($message));
if ($mail->send ())
{
$feedback = "The modification confirmation has been sent to your registered email and the current login has been logged off. \\n Please check the confirmation letter and obtain a new login password. ";
Echo $feedback;
}
?>
Write a bat file.
@d:\php\cli\php.exe E:\web\mail.php >d:\php\cli\sendmail.log
Pause
Save as: Timesend.bat placed in @d:\php\cli\php.exe directory
Add a scheduled task to the window and then you can!
V. Description.
1, I send the message with a template is stored in the database, there are two e-mail delivery classes are not provided, you can contact me.
2, use the absolute path when using Requrie_once.
3, PHP command-line mode allows the PHP script to be completely independent of the WEB server run alone, so if you send a large number of messages can reduce the burden on the server.
4, once again, we recommend that you look at PHP manual 23rd. PHP command-line mode.
This is not really the way to implement automatic mail delivery, but in a web-free way without a desktop application, this can be a better way to go! , I think that the real implementation of automatic mail delivery system, on the server side or there is a desktop application to do support! Therefore, this implementation of automatic mail delivery is only a way to implement the PHP program to send mail!
. $mailtemplate [0][' Mt_message ']. "
";
<?php
Ignore_user_abort (); The PHP script can continue to execute even if the client disconnects (such as turning off the browser).
Set_time_limit (0); Execution time is unlimited, php default execution time is 30 seconds, through Set_time_limit (0) can allow the program to execute indefinitely
$interval = 20;//time interval per second
$key _file= "Key.txt"; Configuration file
If (isset ($_get[')))
{
if ($_get[' s ']== "0") {//stop working, but do not 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 = "<?php\n return \" ". $s." \ ";\n?>";
Write_inc ($key _file, $string, true);
exit ();
}
if (file_exists ($key _file)) {
bo=
$mkey = include $key _file;
if ($mkey = = "true") {//if work
Working Interval//////////////////////////////////
$showtime =date ("y-m-d h:i:s");
$fp = fopen (' Func.txt ', ' a ');
Fwrite ($FP, $showtime. " \ n ");
Fclose ($FP);
///////////////////////////////////////////////////////////////////
}
ElseIf ($mkey = = "Die") {//if exiting
Die ("I am dying!");
}
Sleep ($interval); Wait $interval minutes
}while (TRUE);
}
Else
Die ($key _file. "Doesn ' t exist!");
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);
}
?>
Several ways to perform tasks regularly in PHP