Example of online user count statistics in PHP
This example describes how to count the number of online users in PHP. We will share this with you for your reference. The details are as follows:
I remember using the application object to count online users in ASP. How to Design PHP?
PHP encapsulates session objects very well. According to the HTTP protocol, visitors of each range of websites can generate a unique identifier.
echo session_id();//6ed364143f076d136f404ed93c034201<br />
This is the key to counting the number of online users. Only the session_id can be used to distinguish the accessed users. Because each person is different.
Next, how to save the values in the session variable to the database. Here we will introduce another function
Bool session_set_save_handler (callable $ open, callable $ close, callable $ read, callable $ write, callable $ destroy, callable $ gc) // callable can be withdrawn at any time, and the request is pay-as-you-go, // open (string $ savePath, string $ sessionName) at any time to open the connection // close () close the connection // read (string $ sessionId) output data // write (string $ sessionId, string $ data) // write data // destroy ($ sessionId) // delete data // gc ($ lifetime) // garbage collection function
Note:There are several functions with parameters passed in, as long as you indicate that there is a transfer in. PHP automatically reads the code when executing it.
Parameters in the session
The next step is to complete the above five functions and one 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 completed in this way, but I really don't understand why I use array ("session", "method") to call these methods.
(Basic understanding: all methods of passing objects as parameters must use this form: array (object, "method name "))
The next step is to compile each function.
// Openfunction open ($ path, $ sessname) for linking data {$ db = mysql_connect ("localhost", "root", "123456", "test "); mysql_select_db ("test", $ db); mysql_query ("set names UTF8"); return true ;}
Close data that can be linked
function close(){$db = mysql_connect("localhost","root","123456","test");mysql_close($db);return true;}
The key function is about to begin. It displays the read function read (). The main function is that the read () function has a value passed in, and the input is 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 to write the function. If there is data in the database, it only takes the update time to write new data.
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;}
The next step is to reflect the PHP recycle mechanism function. Both functions have parameter input.
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;}
All right, the five functions are completed, and the number of records of the session is read in the session table. The number of people accessing the page can be accurately calculated.
User records with no operation will be cleared in 10 minutes