亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

php detect connection status

標簽:
PHP

PHP script executed terminal


  • PHP script can be possiblly terminaled in those situations:

1, Max time limiation is reaching.

2, The user hit "stop" button, and in backup server, ignore_user_abort is not setted, when user output something, the php script will stop to be executed.

3, A whole php script page has been executed.


  • Then how to detect connections has been stoped by client PHP internal.

1, do not set ingnore_user_abort to true.

2, Output something,

3, Use "register_shutdown_function" to register a function to be called when PHP script stopped to be executed.

4, Use "connection_status" to return the current connection status to determine following action.


  • Connection status has 3 options

0 - NORMAL

1 - ABORTED

2 - TIMEOUT


In comet functions, when call PHP script, there will be while loop in php script and will never been stopped if server is not returned. But at the same time, client may close the connection to server, but PHP can not detect it. If so, there will be much resource wasted in server. In my project, I connect to mysql server by persistent connection. Because no script to detect connection, when use refresh the page(Especially in development phase), a database connection will be kept, this made the system print out a error say"too many connections to mysql server". So in those application, we must have machanism to detect connection status. Code example:


if($mysql->connect_errno > 0){

$rs['status'] = false;

 $rs['data']  = '链接数据库失败';

 echo json_encode($rs);

 exit;

}

function close($mysql=null){

 if($mysql){

   $mysql->close();

 }

}

register_shutdown_function('close');

$mysql->query("SET NAMES 'utf8'");

while(1){

 $rs = array();

 $result = $mysql->query("SELECT count(0) AS cnt FROM modoer_tmp_products WHERE status = '1'");

 $row = $result->fetch_assoc();

 if($row['cnt'] > 0){

   $rs['count'] = $row['cnt'];

   $rs['status'] = true;

   echo json_encode($rs);

   exit;

 }

 else{

   echo '0';

   flush();

   ob_flush();

   sleep(1);

 }

}

Please note that, in this way, client(browser) will receive some unuseful string(0). To make program work properly, the client should handle it.

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消