Php + memcache implements the online website count statistics code. I want to show the number of online users in my blog statistics. I have found many examples on the Internet, such as database storage data or file storage, and the code looks too complex. I have nothing to worry about in the evening. I want to show the number of online users in my blog statistics. I have found many examples on the Internet, such as database storage data or file storage, and the code looks too complex.
When I came back in the evening, I thought about it. I saw the Memcache service installed on my server. why not use Memcache.
The implementation process is as follows:
:
Implementation code:
<? Php $ mc = new Memcache (); // connect to memcache $ mc-> connect ("127.0.0.1", 11211 ); // get online user IP address and online Time Data $ online_members = $ mc-> get ('online _ members '); // if it is null, initialize the data if (! $ Online_members) {$ online_members = array ();} // obtain the user ip address $ ip = $ _ SERVER ["REMOTE_ADDR"]; // reset the online time for the access user $ online_members [$ ip] = time (); foreach ($ online_members as $ k => $ v) {// if the page is not accessed after three minutes, it will be deemed as expired if (time ()-$ v> 180) {unset ($ online_members [$ k]);} // reset the online user data $ mc-> set ('online _ members ', $ online_members ); // Obtain online user data again $ online_members = $ mc-> get ('online _ members '); // input echo count ($ onli Ne_members);?>
Bytes. I have found many examples on the Internet, such as database storage data or file storage, and the code looks too complex. Back at night...