Php is a method that is executed only when it is started for the first time every day.

Source: Internet
Author: User
I don't know if you have encountered such a problem, that is, you want to write a program to execute the program, such as a few scheduled executions every day, php web programs, the so-called execution must be accessed. Ignore_user_abort (true); function allows the program to continue in the background after the browser is closed... syntaxHighlighter. all (); I don't know if you have ever encountered such a problem, that is, you want to write a program to execute a plan, such as the scheduled execution of several times a day, php web programs, the so-called execution must be accessed. Ignore_user_abort (true) is available. the function allows the program to continue execution in the background when the browser is closed. I have previously written a similar automatic execution method, which can also implement regular execution, however, this method has some disadvantages after all, so I also think that sometimes the system may be able to return to the next step based on different requirements, so that the system can execute the first login every day.
If this method is not suitable for your needs, we should study it carefully. If your function simply changes the attributes of some data, the data does not have to be changed every day, however, it is appropriate to do so when it enters the system to display the latest status. It is not suitable for functions that others cannot access the execution Scheduler. they can only access the scheduler. Let's talk about how this implementation method works.
We can use a txt file to store the last operation time of this function in the form of yyyy-mm-dd, write a function to get the current time, it is also converted to the form of yyyy-mm-dd. for comparison, if it is different, the operation you want will be executed. if it is the same, the operation will not be executed, after Execution, write the current time to the txt file in the form of yyyy-mm-dd. In this way, users logging on to the system will execute this method each time they log on to the system. However, considering that such execution may involve a large amount of data, we can do this by setting the maximum execution time of this method to an infinite length and setting it to be executed in the background. The front-end user logs on to the home page and loads his own page and then uses ajax to access this page. does this solve the efficiency problem.
Next, let's take a look at my code. you can also write a configuration file in the background to check whether the automatic execution of this program is enabled.
// Automatic data transfer
Function turnMessage (){
Set_time_limit (0 );
Ignore_user_abort (true );
Global $ gzhuan, $ guser, $ hfdate1;
If ('1' = $ gzhuan ){
$ Now = time ();
$ Time = date ('Y-m-D', $ now );
$ File = 'include/time.txt ';
$ F = fopen ($ file, 'R + ');
$ T = fread ($ f, filesize ($ file ));
Fclose ($ f );
If ($ t! = $ Time ){
$ Timeok = date ('Y-m-D', $ now-$ hfdate1 * 86400 );
$ User = $ this-> tmp_mod-> getOneUser ($ guser );
If ($ user ){
$ List = $ this-> tmp_mod-> getGuoqi ($ timeok );
If ($ list [0]) {
$ Ymd = date ('Y-m-d H: I: S', $ now );
$ D ['adminid'] = $ guser;
$ D ['admin'] = $ user ['adminrealname'];
$ D ['hfzt '] = 1;
$ D ['yytime'] = $ time;
$ D ['zhuanru '] = 1;
Foreach ($ list as $ key => $ val ){
$ Data ['Pro _ id'] = $ val ['id'];
$ Data ['admin _ id'] = 0;
$ Data ['adminname'] = 'system ';
$ Data ['content'] = 'the system automatically transfers data to this data. the original reservation time is: '. $ val ['yytime'];
$ Data ['dates'] = $ ymd;
$ Data ['type'] = 0;
$ Rs = $ this-> tmp_mod-> addProBz ($ data );
$ Rs2 = $ this-> tmp_mod-> editProducts ($ val ['id'], $ d );
}
If ($ rs & $ rs2 ){
Parent: innerLog ('automatic data transfer ', '1 ');
$ F = fopen ($ file, 'W + ');
Fwrite ($ f, $ time );
Fclose ($ f );
} Else {
Parent: innerLog ('automatic data transfer ', '0 ');
}
}
}
}
}
Unset ($ gzhuan, $ guser, $ hfdate1 );
Exit;
}
The above $ this-> tmp_mod-> and other methods are all the methods used to operate the database in the framework I use, so you do not need to go into it. I think it is a good choice to use ajax for retrieval without affecting user efficiency and achieving our goal.

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.