php if终止,php判断用户是否掉线及关闭网页的方法分享
要實現(xiàn)判斷用戶已掉線并關(guān)閉網(wǎng)頁,主要用到方法connection_status 和 connection_aborted。
通過一個例子,來了解下它們的用法:
echo str_repeat(" ",300);
//以下不可省略,否則用戶斷線,php(線程)立即終止,不會執(zhí)行" if connection_status()!=0||connection_aborted){"
ignore_user_abort(true);
while (true) {
echo "test
/n;//必須有輸出, 否則線程會一直執(zhí)行下去,直到重新啟動apche(測試過程2小時),輸出也可以寫到//注釋2處
flush();
if (connection_status()!=0||connection_aborted()){
//用戶退出了
}
//注釋2
sleep(2);
//by www.jbxue.com
}
?>
下面是另一個例子:
//檢測是否掉線,然后關(guān)閉網(wǎng)頁
//編輯: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
}
}
?>
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的php if终止,php判断用户是否掉线及关闭网页的方法分享的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 冒险家归来礼盒是否半月不上线即可获得
- 下一篇: 《赠顾墨曹诗》第一句是什么