《PHP應(yīng)用:PHP實現(xiàn)統(tǒng)計在線人數(shù)功能示例》要點:
本文介紹了PHP應(yīng)用:PHP實現(xiàn)統(tǒng)計在線人數(shù)功能示例,希望對您有用。如果有疑問,可以聯(lián)系我們。
本文實例講述了PHP實現(xiàn)統(tǒng)計在線人數(shù)的方法.分享給大家供大家參考,具體如下:PHP實例
我記得ASP里面統(tǒng)計在線人數(shù)用application 這個對象就可以了.PHP怎么設(shè)計?PHP實例
PHP對session對象的封裝的很好,根據(jù)HTTP協(xié)議,每個范圍網(wǎng)站的訪客都可以生成一個唯一的標識符PHP實例
echo session_id(); //6ed364143f076d136f404ed93c034201<br />
這個就是統(tǒng)計在線人數(shù)的關(guān)鍵所在,只有有這個session_id 也就可以區(qū)分訪問的人了.因為每一個人都不同.
接下來,是怎么把session變量里面的值存到數(shù)據(jù)庫里面去,這里有將介紹另一個函數(shù)PHP實例
bool session_set_save_handler ( callable $open , callable $close , callable $read , callable $write , callable$destroy , callable $gc ) //callable 可隨時支取的,請求即付的,隨時可償還的 // open(string $savePath, string $sessionName) 打開連接 //close() 關(guān)閉連接 //read(string $sessionId) 對出數(shù)據(jù) //write(string $sessionId, string $data) //寫入數(shù)據(jù) //destroy($sessionId) //刪除數(shù)據(jù) //gc($lifetime) //垃圾回收函數(shù)
注意,上面有幾個函數(shù)是有參數(shù)傳入的,你只要表明有傳送傳入就是的.PHP在執(zhí)行代碼的時候會自動讀取PHP實例
session中對于的參數(shù)PHP實例
接下來就是完成上面五個函數(shù)和一個主函數(shù)就可以了PHP實例
session_set_save_handler( array("session","open"), array("session","close"), array("session","read"), array("session","write"), array("session","destroy"), array("session","gc") );
主函數(shù)就這樣完成了,但為什么要用array(“session”,"方法")來調(diào)用這些方法,我真心搞不懂PHP實例
(基本懂了:凡是將對象的方法作為參數(shù)傳遞都需要使用這種形式:array(對象, "方法名"))PHP實例
接下來就是每個函數(shù)的編寫PHP實例
//鏈接數(shù)據(jù)的open function open($path,$sessname) { $db = mysql_connect("localhost","root","123456","test"); mysql_select_db("test",$db); mysql_query("SET NAMES UTF8"); return true; }
關(guān)閉數(shù)據(jù)可以鏈接的closePHP實例
function close(){ $db = mysql_connect("localhost","root","123456","test"); mysql_close($db); return true; }
關(guān)鍵函數(shù)要開始了,顯示讀取函數(shù)read(),主要,read()函數(shù)是有值傳進去的,傳入的是session_idPHP實例
function read($sid){ $sql = "select data from session where sid='{$sid}' and card='".self::$card."'"; $query = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($query); $row>0?$row["data"]:" "; }
第二個是寫入函數(shù),如果數(shù)據(jù)庫里面存在的數(shù)據(jù),只要更新時間就可以了,新數(shù)據(jù)寫入PHP實例
function write($sid,$data) { $sql = "select sid from session where sid='{$sid}' and card='".self::$card."'"; $query = mysql_query($sql) or die(mysql_error()); $mtime = time(); $num = mysql_num_rows($query); if($num){ $sql = "UPDATE session SET data='{$data}', mtime ='{$mtime}'"; }else{ $sql = "INSERT INTO session (sid,data,mtime,ip,card) VALUES('{$sid}','{$data}','".time()."','{$_SERVER['REMOTE_ADDR']}','".self::$card."')"; } mysql_query($sql); return true; }
接下來就是體現(xiàn)PHP回收機制的函數(shù)了,兩個函數(shù)都有參數(shù)傳入.PHP實例
function destroy($sid){ $sql = "DELETE FROM session WHERE sid='{$sid}'"; mysql_query($sql) or die(mysql_error()); return true; } function gc($max_time){ $max_time = 600; $sql = "DELETE FROM session WHERE `mtime`<'".(time()-$max_time)."'"; mysql_query($sql) or die(mysql_error()); return true; }
好了,五個函數(shù)都完成了,再就是session表中間讀出session的記錄條數(shù)了.就能準確的統(tǒng)計出正在訪問頁面的人數(shù).PHP實例
10分鐘沒有操作的用戶記錄將被清空PHP實例
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》PHP實例
希望本文所述對大家PHP程序設(shè)計有所幫助.PHP實例
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/3037.html