php實(shí)現(xiàn)當(dāng)前用戶在線人數(shù)
原理:根據(jù)不同的IP統(tǒng)計(jì)出當(dāng)前有多少人在線。實(shí)現(xiàn)方式:可以用數(shù)據(jù)庫,也可以用文本。我這里用了文本實(shí)現(xiàn)。<?php/***@ Date 2010.04.07*@ Author;;;;華夏之星 PHP100.com*@ Blog http://hi.baidu.com/woaidelphi/blog*/$user_online = "count.php"//保存人數(shù)的文件touch($user_online);//如果沒有此文件,則創(chuàng)建$timeout = 30;//30秒內(nèi)沒動作者,認(rèn)為掉線$user_arr = file_get_contents($user_online);$user_arr = explode('#',rtrim($user_arr,'#'));print_r($user_arr);$temp = array();foreach($user_arr as $value){$user = explode(",",trim($value));if (($user[0] != getenv('REMOTE_ADDR')) && ($user[1] > time())) {//如果不是本用戶IP并時(shí)間沒有超時(shí)則放入到數(shù)組中array_push($temp,$user[0].",".$user[1]);}}array_push($temp,getenv('REMOTE_ADDR').",".(time() + ($timeout)).'#'); //保存本用戶的信息$user_arr = implode("#",$temp);//寫入文件$fp = fopen($user_online,"w");flock($fp,LOCK_EX); //flock() 不能在NFS以及其他的一些網(wǎng)絡(luò)文件系統(tǒng)中正常工作fputs($fp,$user_arr);flock($fp,LOCK_UN);fclose($fp);echo "當(dāng)前有".count($temp)."人在線"?>
相關(guān)文章:
1. 編程語言PHP在Web開發(fā)領(lǐng)域的優(yōu)勢在哪?2. 理解PHP5中static和const關(guān)鍵字3. php模擬實(shí)現(xiàn)斗地主發(fā)牌4. 使用Canal實(shí)現(xiàn)PHP應(yīng)用程序與MySQL數(shù)據(jù)庫的實(shí)時(shí)數(shù)據(jù)同步5. phpstorm恢復(fù)默認(rèn)設(shè)置的方法步驟6. PHP中file_get_contents設(shè)置header請求頭,curl傳輸選項(xiàng)參數(shù)詳解說明7. Ubuntu下使用 apt-get install 安裝php擴(kuò)展庫mcrypt、curl、gd8. phpstorm恢復(fù)刪除文件的方法9. PHP文件運(yùn)行步驟以及數(shù)據(jù)庫的連接代碼10. Nginx+php配置文件及原理解析

網(wǎng)公網(wǎng)安備