1. 二宝博客首页
  2. php

也谈网站在线人数统计

PHP代码 
  1. db层:    
  2. db_online.PHP    
  3.   
  4. //该函数使用户自己在线,并且检查其他用户的在线情况    
  5. function checkOnline($userid,$tempid=null)    
  6. {    
  7. $conn = connect();    
  8.   
  9. //对于所有用户    
  10. //先设置自己为在线    
  11. $stmt = “UPDATE “.DB_NAME.“.USER SET IsOnline=’Y’ WHERE UserID=”.$userid;    
  12. $result = query($stmt,$conn);    
  13. //info($stmt);    
  14. //如果当前用户是游客    
  15. if ($tempid != null)    
  16. {    
  17. $stmt = “SELECT TempID FROM “.DB_NAME.“.TEMPUSER WHERE   
  18.  
  19. TempID=”.$tempid;    
  20. $result = query($stmt,$conn);    
  21. //info($stmt);    
  22. //如果该游客还在线    
  23. if ($row = fetch_array($result))    
  24. {    
  25. $stmt = “UPDATE “.DB_NAME.“.TEMPUSER SET   
  26.  
  27. RequestTime='”.getCurrentTime().“‘ WHERE TempID=”.$tempid;    
  28. $result = query($stmt,$conn);    
  29. //info($stmt);    
  30. }    
  31. //该游客已经离线    
  32. else    
  33. {    
  34. $stmt = “INSERT INTO “.DB_NAME.“.TEMPUSER   
  35.  
  36. VALUES(‘”.$tempid.“‘,'”.getCurrentTime().“‘)”;    
  37. $result = query($stmt,$conn);    
  38. //info($stmt);    
  39. }    
  40. }    
  41.   
  42. //查看其他用户    
  43. //普通用户    
  44. $stmt = “UPDATE “.DB_NAME.“.USER SET IsOnline=’N’ WHERE “.time().” –   
  45.  
  46. unix_timestamp(RequestTime) > “.ONLINE_DURATION.” AND UserGroupID != “.GUEST;    
  47. $result = query($stmt,$conn);    
  48.   
  49. //游客    
  50. $stmt = “DELETE FROM “.DB_NAME.“.TEMPUSER WHERE “.time().” –   
  51.  
  52. unix_timestamp(RequestTime) > “.ONLINE_DURATION;    
  53. $result = query($stmt,$conn);    
  54. disconnect($conn);    
  55. }    
  56.   
  57. //得到在线人数,分用户和游客    
  58. function getOnlineNumber()    
  59. {    
  60. $olnum = array();    
  61. $conn = connect();    
  62. $stmt = “SELECT COUNT(UserID) FROM “.DB_NAME.“.USER WHERE IsOnline=’Y’ AND   
  63.  
  64. UserGroupID != 4″//4 为guest的用户组id    
  65. //info ($stmt);    
  66. $result = query($stmt,$conn);    
  67. $olnum[‘user’] = result($result,0,“COUNT(UserID)”);    
  68. $stmt = “SELECT COUNT(TempID) FROM “.DB_NAME.“.TEMPUSER”;    
  69. //info ($stmt);    
  70. $result = query($stmt,$conn);    
  71. if ($row = fetch_array($result))    
  72. {    
  73. $olnum[‘guest’] = $row[‘COUNT(TempID)’];    
  74. }    
  75. disconnect($conn);    
  76. return $olnum//from www.w3sky.com   
  77. }    
  78.   
  79.     
  80.   
  81. 其中的connect(), disconnect(), query(),fetch_array()函数在dbmanager.inc.PHP中    
  82. dbmanager.inc.PHP    
  83.   
  84. define(“DB_NAME”,“databasename”);    
  85. define(“DB_USER”,“user”);    
  86. define(“DB_PASS”,“pass”);    
  87. define(“DB_HOST”,“localhost”);    
  88.   
  89. function connect()    
  90. {    
  91. //echo “Connecting to Host:”.HOST.”
    “; 
      
  92. $conn = mysql_connect(DB_HOST,DB_USER,DB_PASS);    
  93. mysql_select_db(DB_NAME);    
  94. /*   
  95. if ($conn)   
  96.  
  97. echo “Connect to database sucessfully. connection id:”.$conn.”
    “; 
     
  98.  
  99. else   
  100.  
  101. echo “Connect to database failed.
    “; 
     
  102.  
  103. */    
  104. return $conn;    
  105. }    
  106.   
  107. function pconnect()    
  108. {    
  109. return mysql_pconnect(DB_HOST,DB_USER,DB_PASS);    
  110. }    
  111.   
  112. function disconnect($conn)    
  113. {    
  114. $close = mysql_close($conn);    
  115. /*   
  116. if ($close)   
  117. echo “MySQL Database disconnected.
    “; 
     
  118. else   
  119. echo “MySQL Database disconnecting failed. Please try again.
    “; 
     
  120. */    
  121. }    
  122.   
  123. function query($stmt,$conn)    
  124. {    
  125. return mysql_query($stmt,$conn); //from www.w3sky.com   
  126. }    
  127.   
  128. function fetch_array($result)    
  129. {    
  130. return mysql_fetch_array($result);    
  131. }    
  132.   
  133. function fetch_row($result)    
  134. {    
  135. return mysql_fetch_row($result);    
  136. }    
  137.   
  138. function num_rows($result)    
  139. {    
  140. return mysql_num_rows($result);    
  141. }    
  142.   
  143. function result($result,$row,$field)    
  144. {    
  145. return mysql_result($result,$row,$field);    
  146. }    
  147.   
  148.   
  149. rule层:    
  150. rl_online.PHP    
  151. function getOnline()    
  152. {    
  153. if ($userid == 2)    
  154. {    
  155. if (session_is_registered(“tempuserid”))    
  156. {    
  157. checkOnline($userid,$tempuserid);    
  158. }    
  159. }    
  160. else    
  161. {    
  162. checkOnline($userid);    
  163. }    
  164. return getOnlineNumber();    
  165. }    
  166.   
  167. ui层:    
  168. ui_online.PHP    
  169. $online_num = getOnline();    
  170. echo “在线人数,注册用户”.$online_num[‘user’].“人,游客”.$online_num[‘guest’].“人”;    
]]>

原创文章,作者:键盘游走者,如若转载,请注明出处:http://www.708034.com/2007/10/%e4%b9%9f%e8%b0%88%e7%bd%91%e7%ab%99%e5%9c%a8%e7%ba%bf%e4%ba%ba%e6%95%b0%e7%bb%9f%e8%ae%a1/

发表评论

电子邮件地址不会被公开。 必填项已用*标注