This article mainly introduced the PHP implementation statistics online number function, combined with the example form analysis PHP based on the session object calculation and statistics online number of related operations skills, the need for friends can refer to the following
In this paper, we describe the method of PHP to realize statistics on-line people. Share to everyone for your reference, as follows:
I remember the ASP to count the number of online people with application this object can be. How does PHP design?
PHP encapsulates the session object very well, according to the HTTP protocol, each scope of the site's visitors can generate a unique identifier
Echo session_id ();//6ed364143f076d136f404ed93c034201<br/>
This is the key to statistics on the number of people online, only the session_id can distinguish the visitors. Because each person is different.
Next, how to save the value of the session variable inside the database, there will be another function introduced
BOOL Session_set_save_handler (callable $open, callable $close, callable $read, callable $write, Callable$destroy, C Allable $GC)//callable can be withdrawn at any time, the request payable, at any time reimbursable//open (String $savePath, String $sessionName) Open the Connection//close () Close the connection//read ( String $sessionId) pairs out data//write (string $sessionId, String $data)//write Data//destroy ($SESSIONID)//delete data//gc ($lifetime)// Garbage collection functions
Note that there are several functions that have parameters passed in, and you just have to indicate that there is an incoming pass. PHP is automatically read when executing the code
Parameters for the session
The next step is to complete the above five functions and a main function.
Session_set_save_handler (Array ("Session", "open"), Array ("session", "Close"), Array ("session", "read "), Array (" session "," write "), Array (" session " ," destroy "), Array (" Session "," GC "));
The main function is done, but why use Array ("Session", "method") to invoke these methods, I really do not understand
(Basically understand: it is necessary to use this form when passing an object's method as a parameter: Array (object, "method name"))
Next is the writing of each function
Link Data openfunction open ($path, $sessname) { $db = mysql_connect ("localhost", "root", "123456", "Test"); mysql_select_db ("Test", $db); mysql_query ("SET NAMES UTF8"); return true;}
Closed data can be linked by close
function Close () {$db = mysql_connect ("localhost", "root", "123456", "Test"); Mysql_close ($db); return true;}
The key function to start, the Read function reads (), the main, read () function is a value passed in, the incoming session_id
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"]: "";}
The second is the write function, if the database exists in the data, as long as the update time, the new data to write
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;}
Next is the function that embodies the PHP recovery mechanism, and two functions have parameters passed in.
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 = +; $sql = "DELETE from session WHERE ' Mtime ' < '". (Time ()-$max _time). "'"; mysql_query ($sql) or Die (Mysql_error ()); return true;}
OK, five functions are finished, and then the session table in the middle of the session to read the number of records. You can accurately count the number of people who are visiting the page.
10 minutes No action user record will be emptied