php實(shí)現(xiàn)當(dāng)前用戶在線人數(shù)
原理:根據(jù)不同的IP統(tǒng)計(jì)出當(dāng)前有多少人在線。實(shí)現(xiàn)方式:可以用數(shù)據(jù)庫(kù),也可以用文本。我這里用了文本實(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)沒動(dòng)作者,認(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來寫記數(shù)器(詳細(xì)介紹)2. PHP設(shè)計(jì)模式(九)外觀模式Facade實(shí)例詳解【結(jié)構(gòu)型】3. Thinkphp3.2.3反序列化漏洞實(shí)例分析4. PHP之道——代碼風(fēng)格指南5. Thinkphp5文件包含漏洞解析6. 如何通過PHP實(shí)現(xiàn)Des加密算法代碼實(shí)例7. PHP Laravel門面的實(shí)現(xiàn)原理詳解8. PHP中file_get_contents設(shè)置header請(qǐng)求頭,curl傳輸選項(xiàng)參數(shù)詳解說明9. ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示(完整代碼)10. ThinkPHP5 框架引入 Go AOP,PHP AOP編程項(xiàng)目詳解
