search
Homephp教程php手册后台PHP脚本判断客户端是否中断(数据是否真正的发到前端)

这个问题,搜索一下,不知道有多少种回答,但是很多回答的都不准确,我测试总结一下 首先是都认可的要判断connection_status()或connection_aborted(),但是这两个状态的,必须是向前端发送数据后,才能获得,即无法被动获得,只能主动获得 因此判断客户端是

这个问题,搜索一下,不知道有多少种回答,但是很多回答的都不准确,我测试总结一下

首先是都认可的要判断connection_status()或connection_aborted(),但是这两个状态的值,必须是向前端发送数据后,才能获得,即无法被动获得,只能主动获得

因此判断客户端是否中断的方法也就只能是,后端向前端发送数据后,判断connection_status()或connection_aborted()的返回值


<?php ignore_user_abort(true);  //如果不忽略,那当下面的flush执行后,程序立即退出

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
ob_flush();
flush();
//控制浏览器前端不要缓存结果,每次都要重新查询

$maxvid = $_GET["maxvid"];
error_log(date("[Y-m-d H:i:s]")." > "."maxvid: ".$maxvid."\n", 3 , "/usr/local/apache2219/logs/php_log");

$gmworker = new GearmanWorker();
$gmworker->setTimeout(60000);
$gmworker->addServer();
$gmworker->addFunction("getmsg", "getmsg");

if ( ($gmworker->work()) && ($gmworker->returnCode() == GEARMAN_SUCCESS) )
{
  echo $msg;
  ob_flush();  //必须有,不然是在PHP_BUFFER不会被发送到客户端,也检查不出是否中断
  flush();     //必须设置ignore_user_abort(true),不然程序直接退出了,无法再判断连接状态
  if ( connection_status() ) {
    error_log(date("[Y-m-d H:i:s]")." workload();

  $result = "OK";

  return $result;
}

?>


Statement
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor