WordPress: pure code to realize the online population statistics function of the website

WordPress: pure code realizes the online population statistics function of the website. Add the following code to the place you want to add. The robin Begin theme can be directly added to the enhanced text gadget. After adding, a maplers.dat file will be automatically created. Please do not delete it.

WordPress:纯代码实现网站在线人数统计功能

  1. <? php  
  2. //First of all, you must have the permission to read and write files. The first access may not be displayed, but it can be refreshed normally   
  3. $online_log  =  "maplers.dat" ;   //Save the file of the number of people to the root directory,   
  4. $timeout  = 30; //If the author is not touched within 30 seconds, it is considered offline   
  5. $entries  = file( $online_log );   
  6. $temp  =  array ();   
  7. for  ( $i =0; $i < count ( $entries ); $i ++){  
  8. $entry  =  explode ( "," ,trim( $entries [ $i ]));   
  9. if (( $entry [0] !=   getenv ('REMOTE_ADDR')) && ( $entry [1] > time())) {  
  10. array_push ( $temp , $entry [0]. "," . $entry [1]. "\n" );   //Get the information of other browsers, remove the timeout, and save it in $temp   
  11. }}  
  12. array_push ( $temp , getenv ('REMOTE_ADDR'). "," .(time() + ( $timeout )). "\n" );   //Update viewer's time   
  13. $maplers  =  count ( $temp );   //Calculate the number of people online   
  14. $entries  = implode( "" , $temp );   
  15. //Write File   
  16. $fp  =  fopen ( $online_log , "w" );   
  17. flock ( $fp ,LOCK_EX);  //Flock() does not work properly in NFS and other network file systems   
  18. fputs ( $fp , $entries );   
  19. flock ( $fp ,LOCK_UN);  
  20. fclose( $fp );   
  21. echo   "Number of people online:" . $maplers . People ;   
  22. ?>   

 

 Watson Blog
  • This article is written by Published on November 28, 2017 00:15:19
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/816.html

Comment