<?php
/*
* Cookie Updater (updateCookie.php)
*
* Keeps the user session alive and healthy...
*
*/
/* This file updates the cookie (as long as it exists) to
stop the cookie from expiring mid-session. This is because
you create a cookie that expires an hour from creation at login
but do not update the expiration time when there is activity
in the admin area so if the user is doing something for an hour
in the admin area they end up being logged out after an hour and
losing their work. */
if(isset($_COOKIE['ID_my_site']) && isset($_COOKIE['Key_my_site']) && isset($_COOKIE['Permission_my_site']))
{ setcookie('ID_my_site', $_COOKIE['ID_my_site'], time() + 3600); setcookie('Key_my_site', $_COOKIE['Key_my_site'], time() + 3600); setcookie('Permission_my_site', $_COOKIE['Permission_my_site'], time() + 3600); }
/* Had an idea in regards to how to log activity (I think). I am working on the
presumption you need to know when they were last active? To do this you just need
to add the column 'LastActive' to the table 'staff'
ALTER TABLE `staff` ADD `LastActive` BIGINT NULL
What 'LastActive' will be is a Unix timestamp produced from this page, writing
the last time they were active on this system with the a timestamp.
Below is the modified condition from above.*/
if(isset($_COOKIE['ID_my_site']) && isset($_COOKIE['Key_my_site']) && isset($_COOKIE['Permission_my_site']))
{ mysql_connect("localhost", "ian", "j******") or die(mysql_error()); mysql_select_db("ian") or die(mysql_error()); $sql = "UPDATE staff SET LastActive = " . time() . " WHERE Username = '" . $_COOKIE['ID_my_site'] . "'";
if($query = mysql_query($sql))
{ setcookie('ID_my_site', $_COOKIE['ID_my_site'], time() + 3600); setcookie('Key_my_site', $_COOKIE['Key_my_site'], time() + 3600); setcookie('Permission_my_site', $_COOKIE['Permission_my_site'], time() + 3600); }
else
die('User Tracking Error!'); mysql_close();
}
?>
<?php
/*
* Example of showing time since last active (ExampleLastActive.php)
*
* Shows the Years, Months, Weeks, Days, Hours, Minutes and Seconds since
* the user was last active, using the 'LastActive' column in 'staff'.
*
*
* ex, "iapoxon was last active 20 hours and 3 seconds ago."
*/
function LastActive($time)
{ $context = array(
array(60 * 60 * 24 * 365 , "years"),
array(60 * 60 * 24 * 7, "weeks"),
array(60 * 60 * 24 , "days"),
array(60 * 60 , "hours"),
array(60 , "minutes"),
array(1 , "seconds"),
);
$now = gmdate('U'); $difference = $now - $time;
for ($i = 0, $n = count($context); $i < $n; $i++) {
$seconds = $context[$i][0];
$name = $context[$i][1];
if (($count = floor($difference / $seconds)) > 0) { break;
}
}
$print = ($count == 1) ? '1 ' . substr($name, 0, -1) : $count . " " . $name;
if ($i + 1 < $n) { $seconds2 = $context[$i + 1][0];
$name2 = $context[$i + 1][1];
if (($count2 = floor(($difference - ($seconds * $count)) / $seconds2)) > 0) { $print .= ($count2 == 1) ? ' 1 ' . substr($name2, 0, -1) : " " . $count2 . " " . $name2;
}
}
return $print;
}
/* Now if we were use this in a page (the above is a custom function and needs to be included in whatever page you use it in)
We would need to write the following... */
mysql_connect("localhost", "ian", "j******") or die(mysql_error());mysql_select_db("ian") or die(mysql_error());$sql = "SELECT LastActive FROM staff WHERE Username = 'xamanning'";
if($query = mysql_query($sql))
{ while($user = mysql_fetch_assoc($query))
echo "Xan was last active " . LastActive($user['LastActive']) . " ago.";
}
else
die('User Tracking Error!');mysql_close();
/* The Result I got from doing this produced the following
"Xan was last active 10 minutes 41 seconds ago." (How long it took to write this page) */
?>