Home Backend Development PHP Tutorial PHP积极断开与浏览器的连接

PHP积极断开与浏览器的连接

Jun 13, 2016 am 11:59 AM
connection content length quot

PHP主动断开与浏览器的连接

曾经整理过一篇《关于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秒后执行. ?>
Copy after login


说明:

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();
Copy after login


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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 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)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
What does CONNECTION_REFUSED mean? What does CONNECTION_REFUSED mean? Jul 31, 2023 pm 02:48 PM

CONNECTION_REFUSED is a network connection error that usually occurs when trying to connect to a remote server. When a client device attempts to establish a network connection with a server, and the server rejects the connection request, a CONNECTION_REFUSED error is returned. Common reasons include: the server is not started, the server cannot accept more connection requests, the server firewall blocks the connection, etc.

How to solve connection error How to solve connection error Nov 07, 2023 am 10:44 AM

Solution: 1. Check the network connection; 2. Check the server status; 3. Clear the cache and cookies; 4. Check the firewall and security software settings; 5. Try to use other networks, etc.

PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused Jun 23, 2023 am 08:54 AM

If you encounter the following error message when using PHP to connect to a MySQL database: PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused, then you can try to solve this problem by following the steps below. To confirm whether the MySQL service is running normally, you should first check whether the MySQL service is running normally. If the service is not running or fails to start, it may cause a connection refused error. you can

Use java's String.length() function to get the length of a string Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Introduction to the usage of length function Introduction to the usage of length function Sep 19, 2023 am 11:41 AM

The length function is used to return the number of characters or bytes in a specified string. It can be used to calculate the length of a string for operation and judgment when querying and processing string data. It should be noted that the length function counts the number of characters in the string, not the number of bytes. For multibyte character sets, a character may consist of multiple bytes. Therefore, the length function counts multibyte characters as one character when calculating the string length.

CSS content properties explained: content, counter, and quotes CSS content properties explained: content, counter, and quotes Oct 21, 2023 am 10:16 AM

Detailed explanation of CSS content attributes: content, counter and quotesCSS (cascading style sheets) is an integral part of front-end development. It can help us beautify web pages and enhance user experience. In CSS, there are some special properties that can be used to control the display of text content, including content, counter, and quotes. This article explains these properties in detail and provides specific code examples. 1. content attribute content attribute

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 Jun 13, 2016 am 10:15 AM

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没问题。

See all articles