Home > Backend Development > PHP Tutorial > PHP scheduled task detects user connection status

PHP scheduled task detects user connection status

高洛峰
Release: 2023-03-02 15:32:01
Original
1943 people have browsed it

This article will take you through the relevant content of planned tasks. I hope it will be helpful to you!

Scheduled task

The code is as follows:

ignore_user_abort(); // 用户关闭浏览器程序依然执行 
set_time_limit(0); // 不限制程序运行时间 
$interval = 3; // 程序循环间隔时间秒 

$link = mysql_connect('localhost', 'username', 'paswd'); 
mysql_select_db('test'); 
mysql_query("SET NAMES 'utf8'"); 

do { 
// 用户关闭浏览器停止 start 
echo str_repeat(' ', 4069); // php只在输出时才检查用户连接状态. 一些web服务器的output_buffering默认值是4096字符. 为确保flush()有效, 设置为4069. 
ob_flush(); 
flush(); 
// 用户关闭浏览器停止 end 

$query = "INSERT INTO `test`.`test_demo` (`title`, `content`) VALUES ('计划任务', '" . date("Y-m-d H:i:s", time()) . "')"; 
mysql_query($query); // 采用写入数据库验证程序 
sleep($interval); 
} while (true);
Copy after login

If there is no output, php cannot detect the user connection status. Even if you close the browser program, it will still run until the apache service is stopped or restarted.

The code is as follows:

// 用户关闭浏览器停止 start 
echo str_repeat(' ', 4069); // php只在输出时才检查用户连接状态. 一些web服务器的output_buffering默认值是4096字符. 为确保flush()有效, 设置为4069. 
ob_flush(); 
flush(); 
// 用户关闭浏览器停止 end
Copy after login

Recommended learning: "PHP Video Tutorial"

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template