search
Homephp教程php手册PHP主动断开与浏览器的连接

曾经整理过一篇《关于PHP连接处理中set_time_limit()、connection_status()和ignore_user_abort()深入解析》,是讲解浏览器客户端断开时,服务器PHP脚本的处理。 这篇文章,将讲解一下服务器PHP脚本怎样主动断开与浏览器的连接,主要方法是使用http协议heade

曾经整理过一篇《关于PHP连接处理中set_time_limit()、connection_status()和ignore_user_abort()深入解析》,是讲解浏览器客户端断开时,服务器PHP脚本的处理。

这篇文章,将讲解一下服务器PHP脚本怎样主动断开与浏览器的连接,主要方法是使用http协议header中的Content-Length和Connection

Content-Length的作用:浏览器接收到指定Content-Length大小的消息实体后,则会断开与服务器的连接。
Connection的作用:浏览器接收到Connection的Close或Keep-Alive后,决定是关闭连接还是继续使用当前的连接进下一次请求。

<?php /**
    * 自动断开与浏览器的连接
    * jiaofuyou
    */

    echo &#39;1234567890&#39;;  //向浏览器输出的内容

    {//断开连接的代码  
        $size=ob_get_length();  
        header("Content-Length: $size");  //告诉浏览器数据长度,浏览器接收到此长度数据后就不再接收数据
        header("Connection: Close");      //告诉浏览器关闭当前连接,即为短连接
        ob_flush();  
        flush();  
    }  
    
    error_log(date("[Y-m-d H:i:s]")." > "."start" ."\n", 3 , "/usr/local/apache2219/logs/php_log");
    
    //断开连接后的执行长时间操作      
    sleep(5);  
    
    echo 'test213';//浏览器接收不到了  
    
    error_log(date("[Y-m-d H:i:s]")." > "."end"   ."\n", 3 , "/usr/local/apache2219/logs/php_log");
    //可以查看错误日志是否延迟5秒后执行. 

?>



说明:

1、单独使用Content-length实际上连接并未断开,仅是浏览器停止接收信息,Connection: Close才是真正的告诉浏览器关闭连接。
2、指定Content-Length 对于 file_get_contents 没有任何意义;若想使用,请用curl。


如果想让PHP不停的向浏览器输出内容:

echo "1234567890"
ob_flush();
flush();

这样是不会立刻输出到浏览器的,可以这样

echo "1234567890
     "
//有换行时会立刻输出到浏览器
ob_flush();
flush();

或者:

echo "1234567890"
print str_pad("",10000);  //输出足够多的内容
ob_flush();
flush();


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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!