Home Backend Development PHP Tutorial PHP actively disconnects from the browser_PHP Tutorial

PHP actively disconnects from the browser_PHP Tutorial

Jul 13, 2016 am 10:27 AM
php and about deal with disconnect Browser of connect

PHP actively disconnects from the browser

I once compiled an article "In-depth analysis of set_time_limit(), connection_status() and ignore_user_abort() in PHP connection processing", which explains the processing of server PHP scripts when the browser client is disconnected.

This article will explain how the server PHP script actively disconnects from the browser. The main method is to use Content-Length and Connection in the http protocol header

The role of Content-Length: After the browser receives the message entity with the specified Content-Length size, it will disconnect from the server.

The role of Connection: After the browser receives the Close or Keep-Alive of the Connection, it decides whether to close the connection or continue to use the current connection for the next request.

 /**

* Automatically disconnect from the browser

* jiaofuyou

*/

echo '1234567890'; //Content output to the browser

 {//Disconnect code

$size=ob_get_length();

Header("Content-Length: $size"); //Tell the browser the data length. After the browser receives this length of data, it will no longer receive data

Header("Connection: Close"); //Tell the browser to close the current connection, which is a short connection

ob_flush();

flush();

 }

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

//Perform long-term operations after disconnection

sleep(5);

echo 'test213';//The browser cannot receive it

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

//You can check whether the error log is executed after a delay of 5 seconds.

 ?>

Description:

1. Using Content-length alone does not actually disconnect the connection. It just stops the browser from receiving information. Connection: Close actually tells the browser to close the connection.

 2. Specifying Content-Length has no meaning for file_get_contents; if you want to use it, please use curl.

If you want PHP to continuously output content to the browser:

echo "1234567890"

ob_flush();

flush();

This will not be output to the browser immediately, you can do this

echo "1234567890

 "

//When there is a line break, it will be output to the browser immediately

ob_flush();

flush();

Or:

echo "1234567890"

print str_pad("",10000); //Output enough content

ob_flush();

flush();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/820410.htmlTechArticlePHP actively disconnects from the browser. I once compiled an article "About set_time_limit(), "In-depth analysis of connection_status() and ignore_user_abort()" is to explain the browser client...
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 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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
1670
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

What problems will you encounter when using native select on your phone? What problems will you encounter when using native select on your phone? Apr 30, 2025 pm 03:06 PM

Issues with native select on mobile phones When developing applications on mobile devices, we often encounter scenarios where users need to make choices. Although native sel...

How to configure the character set and collation rules of MySQL How to configure the character set and collation rules of MySQL Apr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

Laravel Live Chat Application: WebSocket and Pusher Laravel Live Chat Application: WebSocket and Pusher Apr 30, 2025 pm 02:33 PM

Building a live chat application in Laravel requires using WebSocket and Pusher. The specific steps include: 1) Configure Pusher information in the .env file; 2) Set the broadcasting driver in the broadcasting.php file to Pusher; 3) Subscribe to the Pusher channel and listen to events using LaravelEcho; 4) Send messages through Pusher API; 5) Implement private channel and user authentication; 6) Perform performance optimization and debugging.

How to rename a database in MySQL How to rename a database in MySQL Apr 29, 2025 pm 04:00 PM

Renaming a database in MySQL requires indirect methods. The steps are as follows: 1. Create a new database; 2. Use mysqldump to export the old database; 3. Import the data into the new database; 4. Delete the old database.

Composer's Purpose: Managing Project Dependencies in PHP Composer's Purpose: Managing Project Dependencies in PHP Apr 30, 2025 am 12:01 AM

We need Composer because it can effectively manage dependencies of PHP projects and avoid the hassle of version conflicts and manual library management. Composer declares dependencies through composer.json and uses composer.lock to ensure the version consistency, simplifying the dependency management process and improving project stability and development efficiency.

See all articles