php获取连接状态,PHP 返回连接的状态位

用户评论:

jorge dot hebrard at gmail dot com (2009-01-26 16:01:28)

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).

ivo_gelov at gmx dot net (2008-04-09 10:01:02)

Good idea for DB engines like MySQL with MyISAM which do not support transactions. But imagine that user requests a page with many (hundred) image thumbnails. Or if a PDF is generated from a HTML on-the-fly. This usually takes several minutes and if the user does not want to wait and aborts connection - PHP continues until all work is done.

In my opinion it will be MUCH more smarter (PHP is mature enough) to use signalling and terminate script immediately when connection is aborted instead of making presumptions that script author will continuously doing output and periodically flushing output buffer just in order to detect connection abort.

Mhm ....

Michael (2005-03-20 21:59:37)

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.

toppi at kacke dot de (2004-06-16 09:06:50)

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"; //

if (connection_status()!=0){

die;

}

}

will work :)

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

Toppi

carlos at fischerinformatica dot com dot br (2002-01-30 18:58:23)

Very very useful!

I was building a chat and I wanted my script to detect when the browser was closed, so the user could be deleted from the online_users table.

echo str_repeat(" ",300);

ignore_user_abort(true); //this way, the user can stop the output, but not the script.

while (true) {

echo "test
\n";

flush();

sleep(2);

if (connection_status()!=0){

include ('dbconnect.inc');

$sql="delete from online_users where online_user=$user";

$sql_exec=pg_exec($vChatDB, $sql);

die(); //kills the script

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值