php if终止,php判断用户是否掉线及关闭网页的方法分享

要实现判断用户已掉线并关闭网页,主要用到方法connection_status 和 connection_aborted。

通过一个例子,来了解下它们的用法:

echo str_repeat(" ",300);

//以下不可省略,否则用户断线,php(线程)立即终止,不会执行" if connection_status()!=0||connection_aborted){"

ignore_user_abort(true);

while (true) {

echo "test
/n;//必须有输出, 否则线程会一直执行下去,直到重新启动apche(测试过程2小时),输出也可以写到//注释2处

flush();

if (connection_status()!=0||connection_aborted()){

//用户退出了

}

//注释2

sleep(2);

//by www.jbxue.com

}

?>

下面是另一个例子:

//检测是否掉线,然后关闭网页

//编辑:www.jbxue.com

echo str_repeat(" ",300);

ignore_user_abort(true); //without this, current apache thread will terminate Immediately,so

the code "if (connection_status()!=0){" will not be executed as the script was broken off!

while (true) {

echo "test
/n";//if there's no any output, this script will execute endless,

which means the current apache thread will not end until you restart apache and connection_status()will

keep 0 and connection_aborted() will keep false.

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、付费专栏及课程。

余额充值