本文實例講述了PHP實現(xiàn)統(tǒng)計在線人數(shù)的方法。分享給大家供大家參考,具體如下:
我記得ASP里面統(tǒng)計在線人數(shù)用application 這個對象就可以了。PHP怎么設(shè)計?
PHP對session對象的封裝的很好,根據(jù)HTTP協(xié)議,每個范圍網(wǎng)站的訪客都可以生成一個唯一的標識符
1
2
|
echo session_id(); //6ed364143f076d136f404ed93c034201<br /> |
這個就是統(tǒng)計在線人數(shù)的關(guān)鍵所在,只有有這個session_id 也就可以區(qū)分訪問的人了。因為每一個人都不同。
接下來,是怎么把session變量里面的值存到數(shù)據(jù)庫里面去,這里有將介紹另一個函數(shù)
1
2
3
4
5
6
7
8
|
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í)行代碼的時候會自動讀取
session中對于的參數(shù)
接下來就是完成上面五個函數(shù)和一個主函數(shù)就可以了
1
2
3
4
5
6
7
8
|
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)用這些方法,我真心搞不懂
(基本懂了:凡是將對象的方法作為參數(shù)傳遞都需要使用這種形式:array(對象, "方法名"))
接下來就是每個函數(shù)的編寫
1
2
3
4
5
6
7
|
//鏈接數(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ù)可以鏈接的close
1
2
3
4
5
|
function close(){ $db = mysql_connect( "localhost" , "root" , "123456" , "test" ); mysql_close( $db ); return true; } |
關(guān)鍵函數(shù)要開始了,顯示讀取函數(shù)read(),主要,read()函數(shù)是有值傳進去的,傳入的是session_id
1
2
3
4
5
6
|
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ù)寫入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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ù)傳入。
1
2
3
4
5
6
7
8
9
10
11
|
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ù)。
10分鐘沒有操作的用戶記錄將被清空
希望本文所述對大家PHP程序設(shè)計有所幫助。