cmgmyr Posted September 28, 2007 Share Posted September 28, 2007 Hey All, I need to track the time online of my users on a site. Just like on here where you can see how many days/hours/minutes you have been logged in. I'm a little stuck on how to start. I've done some searches for this sort of thing but I haven't come up with anything decent. Any ideas or places that you can point me to? Thanks, -Chris Quote Link to comment https://forums.phpfreaks.com/topic/71076-solved-track-users-online-time/ Share on other sites More sharing options...
rarebit Posted September 28, 2007 Share Posted September 28, 2007 Keep a running total of times since last refresh (access), if time since is greater than x then ignore and continue next time Quote Link to comment https://forums.phpfreaks.com/topic/71076-solved-track-users-online-time/#findComment-357361 Share on other sites More sharing options...
cmgmyr Posted September 28, 2007 Author Share Posted September 28, 2007 Sorry, it's been a long week and I can't seem to wrap my head around this. I'm sure I'm making this more complicated then it should be. Any examples of what I could do? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/71076-solved-track-users-online-time/#findComment-357552 Share on other sites More sharing options...
cmgmyr Posted October 1, 2007 Author Share Posted October 1, 2007 *bump* anything? Quote Link to comment https://forums.phpfreaks.com/topic/71076-solved-track-users-online-time/#findComment-359176 Share on other sites More sharing options...
cmgmyr Posted October 2, 2007 Author Share Posted October 2, 2007 Ok, so I figured it out. Here it is if anyone wants to do something like this. 1. make a "logged_time" column somewhere in your database (I put this in my users table) 2. When they log in do something like this: <?php $_SESSION['user_id'] = $userid; $_SESSION['online_time'] = mktime(); ?> 3. On the top of each page...or in your header file have this: <?php if(getUser()){ //<- this checks to see if the user is logged on...you can do this anyway you want onlineTime(); //<- this alters/saves the online time } ?> 4. Here are your main functions <?php //This function logs the time into the database and makes a new time function onlineTime(){ //Make DB connection here $timeout = 5; //this is the timout in minutes $userid = $_SESSION['user_id']; $now = time(); $difference = $now - $_SESSION['online_time']; if($difference <= ($timeout * 60)){ mysql_query("UPDATE users SET logged_time = logged_time + $difference WHERE userid = $userid"); } $_SESSION['online_time'] = mktime(); } //This function calculates and outputs the logged time function showOnlineTime($userid){ //Make DB connection here $result = mysql_query("SELECT logged_time FROM users WHERE userid = $userid"); if(mysql_num_rows($result) > 0){ list($total) = mysql_fetch_row($result); $days = floor($total / 86400); $hours = floor(($total % 86400) / 3600); $minutes = floor(($total % 3600) / 60); echo "$days days, $hours hours and $minutes minutes."; }else{ echo "No Logged Time Available"; } } ?> Simple enough right? Quote Link to comment https://forums.phpfreaks.com/topic/71076-solved-track-users-online-time/#findComment-360336 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.