php一种每天仅第一次启动时执行的方法的实现
不知道大家有没有遇到过这样的问题,就是想要写一个计划执行的程序,比如每天的几点定时的执行,php这种web程序,所谓的执行是必须通过访问的。有ignore_user_abort(true);函数可以让程序在关闭浏览器在后台继续执行,我之前写过一个类似的取巧的自动执行方法,也是可以实现定时执行,但是那种方法毕竟是有弊端的,所以我也再想,有的时候可能根据需求的不同可以退而求其次,让系统每次每天第一登陆的时候去执行。
至于这样的方式适不适合你的需求,我们应该好好的研究一下。如果你的这个功能是单纯的改变一些数据的属性,这些数据并不是必须每天都去改,而是当其进入系统显示成最新状态就可以的就适合这样做。不适合那种别人不能访问到执行计划程序的函数,仅能自己人可以的。无论如何先说一下这种实现方式是如何做的。
我们可以通过一个txt文件来存储最后操作这个函数的时间yyyy-mm-dd这样的形式,写一个函数,获取当前时间,也转化为yyyy-mm-dd这样的形式,对比,如果不同则执行你要的操作,如果相同就不执行,执行之后将当前时间再以yyyy-mm-dd这样的形式写入到txt文件中。这样让登陆系统的用户每次登陆都先执行这个方法。不过,这里考虑到这样的执行可能数据量很大,我们可以这样来做,将这个方法的最大执行时间设置为无限长,并设置为可以在后台执行。前台用户登陆的首页加载完自己的页面后通过ajax来访问这个页面,这样不就解决效率的问题了吗。
下面看一下我的这样一个代码,后台也可以写个配置文件,是否开启自动执行这个程序的功能。
//自动数据转移
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'] = '系统';
$data['content'] = '系统自动转入本数据,原预约时间为:'.$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('自动转移数据','1');
$f = fopen($file,'w+');
fwrite($f,$time);
fclose($f);
}else{
parent::innerLog('自动转移数据','0');
}
}
}
}
}
unset($gzhuan,$guser,$hfdate1);
exit;
}
上面的$this->tmp_mod->等方法都是我用的框架中一下操作数据库的方法,不用深究。我觉得这样写,再用ajax来调取,就会不影响用户的效率,又能达到我们的目的,也是很不错的选择。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
