Home php教程 php手册 php 计划任务 检测用户连接状态

php 计划任务 检测用户连接状态

Jun 06, 2016 pm 08:40 PM
Scheduled Tasks

php 计划任务 检测用户连接状态实现代码,需要的朋友可以参考下

计划任务
代码如下:
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);

如果没有输出php是无法检测用户连接状态的。即使关闭浏览器程序依然会运行直到apache服务停止或重启。
代码如下:
// 用户关闭浏览器停止 start
echo str_repeat(' ', 4069); // php只在输出时才检查用户连接状态. 一些web服务器的output_buffering默认值是4096字符. 为确保flush()有效, 设置为4069.
ob_flush();
flush();
// 用户关闭浏览器停止 end
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)