I hope that the php code can continue to run after the browser disconnects, but I have tried many methods but it doesn’t work
1. Simple, direct and reckless type
1
2
3
4
5
6
7
8
9
10
<?php
ignore_user_abort();//Close the browser, PHP scripts can also be used Continue execution.
set_time_limit(0); // Through set_time_limit(0), the program can be executed without limit
ini_set('memory_limit','512M'); // Set memory Limitation
$interval=60*30;//Run every half hour
do{
//ToDo
sleep($interval); // Wait 5 minutes
}
while(true);
Disadvantages: After starting, it cannot be controlled, Unless you terminate the PHP host. Do not use this method unless you are a hacker.
2. Simple and controllable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
config.php
<?php
return 1;
?>
cron.php
ignore_user_abort();//Close the browser, and the PHP script can continue to execute.
set_time_limit(0);//The program can be executed without limit through set_time_limit(0)
$interval=60*30;//Run every half hour
do{
$run = include 'config.php';
if (!$run) die('process abort');
//ToDo
sleep($interval);//Wait for 5 minutes
}
while(true);
Stop the program by changing return 0 in config.php. A feasible way is to The config.php file interacts with a special form and configures it by setting some variables on the HTML page
Disadvantages: It takes up system resources and runs for a long time, which may cause some unexpected hidden dangers. For example, memory management issues.
3. Simple improved type
1
2
3
4
5
6
7
8
9
< ;?php
$time=15;
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
/*
function
*/
sleep($time);
file_get_contents($url);
? >
php script sleep continues to execute by accessing itself after a period of time. Just like a relay race.. This ensures that each PHP script executes The time will not be too long. It will not be restricted by time_out.
Because each loop of the php file is executed independently, this method avoids the restriction of time_out. But it is best to be the same as above Add control code. cofig.php to be able to terminate the process.
The above method is useless after testing. The code will still stop automatically after running for a few minutes. I personally hope that it can run 24 hours a day. Are there any other methods? Or is there a website that can provide a service that automatically opens web pages, so that my code can be started every once in a while?
大瓶可乐@php.cn2022-06-07 17:21:46
Do you want to be a reptile? You can make a paging jump and jump to the next one after execution!