number users currently online


SUBMITTED BY: phpsnippets

DATE: Oct. 21, 2015, 2:09 p.m.

FORMAT: Text only

SIZE: 1.2 kB

HITS: 1795

  1. <?php
  2. session_start();
  3. include"db.php"; # Connect To the database
  4. $active_sessions = 0;
  5. $minutes = 5; # period considered active
  6. if($sid = session_id()) # if there is an active session
  7. {
  8. # DB connect here
  9. $ip = $_SERVER['REMOTE_ADDR']; # Get Users IP address
  10. # Delete users from the table if time is greater than $minutes
  11. mysql_query("DELETE FROM `active_sessions` WHERE
  12. `date` < DATE_SUB(NOW(),INTERVAL $minutes MINUTE)")or die(mysql_error());
  13. # Check to see if the current ip is in the table
  14. $sql = mysql_query("SELECT * FROM active_sessions WHERE ip='$ip'");
  15. $row = mysql_fetch_array($sql);
  16. # If the ip isn't in the table add it.
  17. if(!$row){
  18. mysql_query("INSERT INTO `active_sessions` (`ip`, `session`, `date`)
  19. VALUES ('$ip', '$sid', NOW()) ON DUPLICATE KEY UPDATE `date` = NOW()")or die(mysql_error());
  20. }
  21. # Get all the session in the table
  22. $sessions = mysql_query('SELECT * FROM `active_sessions`')or die(mysql_error());
  23. # Add up all the rows returned
  24. $active_sessions = mysql_num_rows($sessions);
  25. }
  26. # Print the final result
  27. echo'<b>Online Now: </b>'.$active_sessions;
  28. ?>

comments powered by Disqus