Home Backend Development PHP Tutorial Windows下Apache能否让一个PHP文件每隔几秒执行一次,该怎么解决

Windows下Apache能否让一个PHP文件每隔几秒执行一次,该怎么解决

Jun 13, 2016 pm 01:46 PM
interval limit php quot windows

Windows下Apache能否让一个PHP文件每隔几秒执行一次
请允许我问这个很白痴的问题,不过我琢磨了很长时间也没想到一个很好的解决办法。
背景是这样的:我需要一段PHP代码去定期的对数据库的数据进行操作,并把结果保存起来。假如只是在用户请求的时候去执行这段代码的话,用户的响应时间会很长,用户体验极差。所以我希望能有一个程序专门来执行这段代码。开始的时候想到用windows的计划任务来解决,但是由于执行的频率非常高,用计划任务显然不合适。
所以希望高人指点,若能给个简单的例子就再好不过了,谢谢!

------解决方案--------------------
好像只能用计划任务,或者用PHP来执行 

PHP code
ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行.
set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去
$interval=60*30;// 每隔半小时运行
do{
   //这里是你要执行的代码   
   sleep($interval);// 等待5分钟
}while(true);
<br><font color="#e78608">------解决方案--------------------</font><br>linux有cron,很好使。<br><br>windows下如果mysql5.1以上,有Event Scheduler,不过没用过,LZ自己查下资料<br>
<br><font color="#e78608">------解决方案--------------------</font><br>多种方法:<br>1.用sleep()函数实现类似linux contab定时执行的功能。<br><br>2.linux系统直接在crontab中添加定时任务。
<br><font color="#e78608">------解决方案--------------------</font><br>你可以做一个包含文件,同解一楼,然后在需要的地方包含进这个文件,既省去了不用的时候占用资源,而且随着调用而运行,多好啊,关闭网页之后还可以继续运行。只要任何动作都可触发就可以了吧。用load触发比较好。
<br><font color="#e78608">------解决方案--------------------</font><br>使用js,使网页定时刷新,在网页内读取数据库。只要网页打开,就可以一直运行。而且占用资源少
<br><font color="#e78608">------解决方案--------------------</font><br>用js刷新一个页面。<br><br>设置浏览器的主页为这个页面。<br><br>把浏览器加到windows的开机启动里。<br><br><br><br>
<br><font color="#e78608">------解决方案--------------------</font><br>写测试脚本:<br><?php <br /><br>ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行.<br>set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去<br>$interval=30;// 每隔一定时间运行<br>do{<br><br>	   //这里是你要执行的代码 <br>	$msg=date("Y-m-d H:i:s") ." -执行了~  \n";<br>	file_put_contents("log.log",$msg,FILE_APPEND);<br>  <br>  sleep($interval);// 等待5分钟<br>}while(true);<br><br><br>然后直接就可以在命令行里边执行  <br>c:\php5\php.exe test.php<br><br>可以把这个加入开始开机运行 或加入到系统服务中,就可以自动运行了 <div class="clear">
                 
              
              
        
            </div>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How to update the latest version of Bybit Exchange? Will there be any impact if it is not updated? How to update the latest version of Bybit Exchange? Will there be any impact if it is not updated? Feb 21, 2025 pm 10:54 PM

The way to update ByBit exchanges varies by platform and device: Mobile: Check for updates and install in the app store. Desktop Client: Check for updates in the Help menu and install automatically. Web page: You need to manually access the official website for updates. Failure to update the exchange can lead to security vulnerabilities, functional limitations, compatibility issues and reduced transaction execution efficiency.

deepseek web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

See all articles