This article is an example of how PHP realizes the number of online statistics. Share to everyone for your reference, specific as follows:
I remember the ASP inside statistics online number of people with application this object can be. How does PHP design?
PHP encapsulates the session object well, and according to the HTTP protocol, visitors to each range site can generate a unique identifier
Echo session_id ();
6ed364143f076d136f404ed93c034201<br/>
This is the key to counting the number of people on the line, and only with this session_id can we 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
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, at the request of payment, at any time reimbursable
//open (String $savePath, String $sessionName) Open connection
//close () Closes
the connection//read (string $sessionId) to the data
//write (String $sessionId, String $data)//write Data
//destroy ($sessionId )//delete data
//gc ($lifetime)//garbage collection function
Note that there are several functions that have arguments passed in, and you simply indicate that there is a transfer pass. PHP automatically reads the code when it executes
Parameters for the session
The next step is to complete the top five functions and a main function.
Session_set_save_handler (
Array ("Session", "Open"),
Array (' Session ', ' Close '),
Array ("session", "read "),
Array (" session "," write "),
Array (" Sessions "," destroy "),
Array (" Session "," GC ")
);
The main function is done, but why use Array ("Session", "method") to invoke these methods, I really do not understand
(Basic understand: The object of the method as a parameter to pass all need to use this form: Array (Object, "method name")
The next step is the writing of each function
The Open
function open ($path, $sessname) of the linked data {
$db = mysql_connect ("localhost", "root", "123456", "Test");
mysql_select_db ("Test", $db);
mysql_query ("SET NAMES UTF8");
return true;
}
Close with data can be linked
function Close () {
$db = mysql_connect ("localhost", "root", "123456", "Test");
Mysql_close ($db);
return true;
}
Key function to start, show read (), Main, read () function is a value passed in, the incoming 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 the write function, if the data exist in the database, as long as the update time on it, 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 recycle mechanism, two functions have parameter incoming.
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 complete, and then the session table in the middle of the session to read the number of records. will be able to accurately statistics the number of people who are visiting the page.
10-minute user records without operations will be emptied
More interested in PHP related content readers can view the site topics: "PHP file Operation Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "Basic PHP Grammar Introduction Tutorial", "PHP operation Office Document skills Summary (including Word, Excel,access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.