Home > php教程 > php手册 > body text

后台PHP脚本判断客户端是否中断(数据是否真正的发到前端)

WBOY
Release: 2016-06-06 19:45:23
Original
1146 people have browsed it

这个问题,搜索一下,不知道有多少种回答,但是很多回答的都不准确,我测试总结一下 首先是都认可的要判断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;
}

?>
Copy after login


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!