PHP积极断开与浏览器的连接
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 '1234567890'; //向浏览器输出的内容 {//断开连接的代码 $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();

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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.

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

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.

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

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