irwan

Track and display users online time

Nov 15th, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.49 KB | None | 0 0
  1. <?php
  2. $_SESSION['user_id'] = $userid;
  3. $_SESSION['online_time'] = mktime();
  4. ?>
  5.  
  6.  
  7.  
  8. <?php
  9. if(getUser()){ //<- this checks to see if the user is logged on...you can do this anyway you want
  10. onlineTime(); //<- this alters/saves the online time
  11. }
  12. ?>
  13.  
  14.  
  15.  
  16. <?php
  17.  
  18. //This function logs the time into the database and makes a new time
  19. function onlineTime(){
  20.         //Make DB connection here
  21.        
  22.         $timeout = 5; //this is the timout in minutes
  23.        
  24.         $userid = $_SESSION['user_id'];
  25.        
  26.         $now = time();
  27.         $difference = $now - $_SESSION['online_time'];
  28.         if($difference <= ($timeout * 60)){
  29.                 mysql_query("UPDATE users SET logged_time = logged_time + $difference WHERE userid = $userid");
  30.         }
  31.         $_SESSION['online_time'] = mktime();
  32. }
  33.  
  34. //This function calculates and outputs the logged time
  35. function showOnlineTime($userid){
  36.         //Make DB connection here
  37.        
  38.         $result = mysql_query("SELECT logged_time FROM users WHERE userid = $userid");
  39.         if(mysql_num_rows($result) > 0){
  40.                 list($total) = mysql_fetch_row($result);
  41.                 $days = floor($total / 86400);
  42.                 $hours = floor(($total % 86400) / 3600);
  43.                 $minutes = floor(($total % 3600) / 60);
  44.                 echo "$days days, $hours hours and $minutes minutes.";
  45.         }else{
  46.                 echo "No Logged Time Available";
  47.         }
  48. }
  49.  
  50. ?>
  51.  
  52.  
  53.  
  54. <?php
  55. $userid = 5;
  56. showOnlineTime($userid);
  57. ?>
  58.  
Advertisement
Add Comment
Please, Sign In to add comment