Track and display users online time
5
Well after searching the internet for something similar to this, I didn't come up with anything...so I made my own! This simple script will track your members' time spent on your website. Please feel free to give comments/suggestions/feedback.
<?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
}
?>
<?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";
}
}
?>
<?php
$userid = 5;
showOnlineTime($userid);
?>






I've got good news, and I've got bad news:
The universe is merely a figment of my imagination.
Now are you ready for the bad news?