How to make PHP perform tasks regularly (with code)
This article mainly introduces how to make PHP perform tasks regularly. It has certain reference value. Interested friends can take a look. I hope it will be helpful to you!
If you use PHP to perform certain tasks regularly, you can have the following two methods:
1. crontab under linux, scheduled tasks under windows
2. Use PHP related functions
<span style="font-size: 14px;">set_time_limit(0);<br>ignore_user_abort(true);<br>//这里写一个死循环<br></span>
The first method is the most common. If you don’t have permission to crontab on the PHP server, you can also find a machine of your own to regularly crontab to request the server
The second method is less reliable. Just restart Apache. Have to revisit, fastcgi would be better.
Example: Create index.php and test.txt. The function is to overwrite and write a number to test.txt every second, and the number will increase. The index.php code is as follows:
<span style="font-size: 14px;"><?php<br>ignore_user_abort(true);<br>$num=0;<br>set_time_limit(0);<br>//ini_set('max_execution_time',0); 用这句也行,效果和set_time_limit(0)一样<br>do{<br> file_put_contents('./test.txt',$num);<br> $num++;<br> sleep(1);<br>}while(true);<br></span>
After closing the browser, I found that the script can still be executed, and the number is still increasing.
The reason is that these two key functions are at work:
ignore_user_abort(true) regardless of whether the client closes the browser, the following code will be executed.
set_time_limit(0) Cancel the execution time of the php file. If there is no this function, the default php execution time is 30 seconds, which means that after 30 seconds, this file will say goodbay.
If you do not use these two functions, you need to modify php.ini, find the max_execution_time configuration item, change 30 to 0, and set it to 0 to never expire. Just restart the server.
For more related tutorials, please visit A complete set of video tutorials on PHP programming from entry to master

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

AI Hentai Generator
Generate AI Hentai for free.

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

Validator can be created by adding the following two lines in the controller.
