略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: connection_status

2024-11-14

connection_status

(PHP 4, PHP 5, PHP 7, PHP 8)

connection_status返回连接的状态位

说明

connection_status(): int

获得当前连接的状态位。

返回值

获得当前连接的状态位, 可以用于与 CONNECTION_XXX 定义的常量来确定连接状态。

参见

add a noteadd a note

User Contributed Notes 4 notes

up
38
toppi at kacke dot de
18 years ago
Notice !

if you running a loop (while, foeach etc..)  you have to send something to the browser to check the status.

Example:

while(1){
    if (connection_status()!=0){
    die;
    }
}
doesnt work, if the user break/close the browser.

But a:

while(1){
    Echo "\n"; //<-- send this to the client
    if (connection_status()!=0){
    die;
    }
}
will work :)

i hope it will help some of you to safe some time :)

Toppi
up
0
Anonymous
2 years ago
As mentioned, this function returns a status bitfield to which there's a set of constants available. I don't know why those constants aren't actually listed. Although they're easy to guess, I think it's still worth listing them, it is documentation after all. This function has the ability to return integers 0 through 3 so there are 4 possible states.

The constants are as follows:

CONNECTION_NORMAL = 0
CONNECTION_ABORTED = 1
CONNECTION_TIMEOUT = 2

As a 4th state is possible and being a bitfield, this gives rise to CONNECTION_ABORTED|CONNECTION_TIMEOUT (or integer 3) can be used to check for aborted+timeout states.
up
-7
Michael
17 years ago
Yes it is true. I made some experiments with that functions 'connection_abortes()'. First a source made an error, which I see. They wrote: ignore_user_abort();

But that only gives you the status of the 'Abort-Setting'.
So I try (with little hope)
  'ignore_user_abort(true);'
And as I readout the setting it has changed it...

Next I see that the script runs after I disconnect with the site. But other experiments fail. I try some things and then it
was logical after an experiment: flush() is one of the necessary things. Without those output to the client the function
          'connection_aborted()' stays on 'false'
The Second is that you have to output something. Without that it also doesn't works.
So I now know that you have to echo something and then output the buffer. Only then 'the Script' (or the function)
'knows' that the client is disconnected.
up
-12
jorge dot hebrard at gmail dot com
13 years ago
You can always send chr(0) to check if browser is still alive, that will show no output in browser page (at least in Firefox).

官方地址:https://www.php.net/manual/en/function.connection-status.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3