Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between long connection and short connection in php

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the differences between long and short connections in php", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's take you to learn "what are the differences between long and short connections in php"!

The difference between a long connection and a short connection in php: 1, short connection refers to the SOCKET connection, immediately after sending and receiving data disconnect; and long connection refers to the establishment of SOCKET connection, regardless of whether to use it to maintain the connection until one party closes the connection. Compared with short connections, long connections are less secure.

Operating environment of this tutorial: Windows 7 system, PHP7.1 version, DELL G3 computer

short connection

Connect-> Transfer Data-> Close Connect

HTTP, for example, is a stateless short link. Every time the browser and server perform an HTTP operation, a connection is established, but the connection is interrupted at the end of the task.

Specifically, the browser client initiates and establishes a TCP connection-> the client sends an HttpRequest message-> the server receives the message-> the server handle and sends an HttpResponse message to the front end. After sending, the socket.close method is immediately called-> the client receives a response message-> the client will finally receive a signal that the server side disconnects the TCP connection-> the client side disconnects the TCP connection. Specifically, the close method is called.

It can also be said that: short connection refers to SOCKET connection, send and receive data immediately after the disconnection.

Because the connection is disconnected after receiving data, there is no contact with each data reception process. This is one of the reasons HTTP is stateless.

long connection

Connect-> Transfer Data-> Stay Connected-> Transfer Data->.................................................................................................................................. Until one party closes the connection, mostly the client closes the connection.

A long connection means that after establishing a SOCKET connection, it remains connected regardless of whether it is used or not, but the security is poor.

Every time we access a PHP script, we get results only after all PHP scripts have been executed. If we need a script to run continuously, we need to use php long links to get it running.

The general php environment is apache+php+linux, but because apache has a time limit for php connections, apache servers will automatically disconnect after this connection time.

In this case, the simpler and more convenient way is to set set_time_limit(0) in the php page (of course, you can also set it in the php.ini configuration file, but this will affect the entire environment. After all, we don't want to connect for a long time on all connections and affect service performance)

Every PHP script has a limit on execution time, so we need to set the execution time of a script to infinity with set_time_limit, and then use flush() and ob_flush() to clear the server buffer and output the return value of the script at any time.

When we execute it, we execute it every five seconds, and in this way we can perform many functions, such as background monitoring programs, timed execution functions, log analysis, data sorting and other time-consuming operations.

At this point, I believe that everyone has a deeper understanding of "what is the difference between long and short connections in php", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report