windows定时执行PHP的技能
windows定时执行PHP的技巧
windows定时执行PHP相信不少读者(PHP爱好者)在工作、学习的过程中经常抱怨:在WIN如何让PHP定时自动发信呢??如何让MYSQL实现自动备份而无后顾之忧呢??如 果完全依靠手工进行当然也可以实现,但操作上似乎过于繁琐了一点!别着急,利用系统的任务计划程序(Windows 98称之为计划任务)可轻易解决这一问题。这一功能往往被很多用户忽略或者未曾想到:)。
所谓任务计划就是由计算机自动调用用户事先设置好的应用程序,从而达到简化用户操作的目的。利用Windows 2000的任务计划程序(相当与*NIX下的cron程序,这里不再对其详述),我们可以安排任何脚本、程序或文档在最恰当的时候运行,从而满足自己的需 要。下面以Windows 2000为例。
具体来说,我们若需利用任务计划程序自动运行则应执行如下步骤:
1.单击“开始”按钮,然后依次选择“程序”→“附件”→“系统工具”→“任务计划”(或者是“设置”→“控制面板”→“任务计划”),启动Windows 2000的任务计划管理程序。
2.在“任务计划”窗口中双击“添加任务计划”图标,启动系统的“任务计划向导”,然后单击“下一步”按钮,在给出的程序列表中选择需要自动运行的应用程序,然后单击“下一步”按钮。
设置适当的任务计划名称并选择自动执行这个任务的时间频率(如每天、每星期、每月、一次性、每次启动计算机时、每次登录时等),然后单击“下一步”按钮。
此时系统将会要求用户对程序运行的具体时间进行设置,如几号、几点钟、哪几个时间段才能运行等,我们只需根据自己的需要加以设置即可。
4.接下来系统将会要求用户设置适当的用户名及密码,以便系统今后能自动加以运行。
5.最后,我们只需单击“完成”按钮即可将相应任务添加到Windows 2000的任务计划程序中,此后它就会自动“记住”这个任务,一旦系统时间及相关条件与用户设置的计划相符,它就会自动调用用户所指定的应用程序,十分方 便(每次启动Windows 2000的时候,任务计划程序都会自动启动,并在后台运行,确保用户的计划能够按时执行)。
现在我们来测试一下刚才所建的任务是否成功,鼠标右键单击“php”程序图标(如图6所示),在弹出的菜单里面选择“运行”。一般情况下程序图标只要这样 激活运行就可以正常启动。如果运行失败可查看用户和密码是否设置正确,还有确定“Task Scheduler”服务是否已启动,本人当初就是为了节省系统资源把它关掉了导致运行失败,害我找了大半天。另外也可从“系统日志”里查看到底是什么原 因造成运行失败的。
好了,讲了这么多任务计划的应用,现在我们切入正题,下面将介绍两个例子:
一、让PHP定时运行吧!
1、 编辑如下代码,并保存为test.php:
$fp = @fopen("test.txt", "a+");
fwrite($fp, date("Y-m-d H:i:s") . " 让PHP定时运行吧!\n");
fclose($fp);
?>
打开文本输入:D:\php4\php.exe -q D:\php4\test.php
保存为.bat格式。
D:\php4\php.exe 是php安装路径,D:\php4\test.php是要定时运行的程序的路径。
2、添加一个任务计划,选择那个.bat文件
3、时间设置为每隔1分钟运行一次,然后运行这个任务。
4、现在我们来看看d:\php4\test.txt文件的内容时候是否成功。如果内容为如下所示,那么恭喜你成功了。
2016-03-03 11:08:01 让PHP定时运行吧!
2016-03-03 11:09:02 让PHP定时运行吧!
2016-03-03 11:10:01 让PHP定时运行吧!
2016-03-03 11:11:02 让PHP定时运行吧!
二、让MYSQL实现自动备份变成可能!
1、编辑如下代码,并保存为backup.php,如果要压缩可以拷贝一个rar.exe:
if ($argc != 2 || in_array($argv[1], array('--help', '-?'))) {
?>
backup Ver 0.01, for Win95/Win98/WinNT/Win2000/WinXP on i32
Copyright (C) 2000 ptker All rights reserved. http://www.dareng.com
This is free software,and you are welcome to modify and redistribute it
under the GPL license
PHP Shell script for the backup MySQL database.
Usage:

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 remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

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 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 each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

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.
