Home >Backend Development >PHP Tutorial >PHP implementation of a method that is executed only on the first startup every day_PHP tutorial
I don’t know if you have ever encountered such a problem, that is, you want to write a program that is planned to be executed, such as scheduled execution at certain times every day. The so-called execution of web programs such as PHP must be accessed. There is the ignore_user_abort(true); function that allows the program to continue executing in the background after closing the browser. I have written a similar clever automatic execution method before, which can also achieve scheduled execution, but that method has disadvantages after all, so I Think about it again, sometimes depending on the needs, you can settle for the second best and let the system execute it every time you log in for the first time every day.
As for whether this method is suitable for your needs, we should study it carefully. If your function is to simply change the attributes of some data, the data does not have to be changed every day, but it will be displayed in the latest status when it enters the system. This is suitable for this. It is not suitable for functions that other people cannot access to the execution plan program, only their own people can. Anyway, let’s talk about how this implementation is done.
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 and convert it into the form of yyyy-mm-dd. Compare it and execute it if it is different. If the required operations are the same, they will not be executed. After execution, the current time will be written to the txt file in the form of yyyy-mm-dd. This allows users who log in to the system to execute this method first every time they log in. However, considering that such execution may have a large amount of data, we can do this by setting the maximum execution time of this method to infinite and setting it to be executed in the background. After the homepage where the front-end user logs in loads its own page, it accesses this page through ajax. Doesn't this solve the problem of efficiency?
Let's take a look at my code. You can also write a configuration file in the background to enable the function of automatically executing this program.
//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 this data, the original appointment 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('automatically transfer data','1');
$f = fopen($file,'w+');
fwrite($f,$time);
fclose($f);
}else{
parent::innerLog('automatically transfer data','0');
}
}
}
}
}
unset($gzhuan,$guser,$hfdate1);
exit;
}
The above $this->tmp_mod-> and other methods are all methods I use to operate the database in the framework, so there is no need to go into details. I think writing it like this and using ajax to retrieve it will not affect the user's efficiency and achieve our purpose. It is also a very good choice.