Page 1 of 1
[SOLVED] Counting how long a users been online, code done; l
Posted: Sun Oct 03, 2004 4:07 pm
by tony montana
Ok Ive coded what I think would work - bar the query - before I do that Id like your ideas and feedback.
Basically the function is meant to check the times of the cookie and compare to the limits and if it seems like the users been on the site for say 5 minutes - log that.
I know a cookie can be spoofed, but its only for shts and giggles.
Code: Select all
$forum_properties['clock_timeonline'] = 1;
$forum_properties['clock_timediffo'] = 320;
function clock_timeonline()
{global $user_properties, $forum_properties, $time;
if(($user_properties['loggedIn'] == 1) && ($forum_properties['clock_timeonline'] == 1))
{if(!$_COOKIE['clock_timeonline'])
{setcookie ('clock_timeonline', $time, time()+(44442000*864000), '/',"".$forum_properties['cookieURL']."", 0);}
elseif($_COOKIE['clock_timeonline'] <= ($time + $forum_properties['clock_timediffo'])#if the time plus diffrence is below or equal to the cookie visit
&& (($time - $_COOKIE['clock_timeonline']) <= ($forum_properties['clock_timediffo'] + 50)))#if the diffrence plus 50 seconds is greater than the cookie visit
{echo ($time - $_COOKIE['clock_timeonline']);#testing purposes
#do database query here
setcookie ('clock_timeonline', $time, time()+(44442000*864000), '/',"".$forum_properties['cookieURL']."", 0);}}
return $var;}
$var = clock_timeonline();
echo $var;
returns $var for testing purposes atm.
Posted: Sun Oct 03, 2004 4:10 pm
by John Cartwright
I think you should seriously rethink how you organize your code.
Posted: Sun Oct 03, 2004 4:12 pm
by feyd
syntax error. unmatched '{'.
generally I just track when they logged in, and the last time they accessed a page. Subtract the two and you have the length of time they have been on.
Posted: Sun Oct 03, 2004 4:37 pm
by tony montana
That wouldnt accurately count page to page time online ness.
I have users online - location, time, referer and all that stuff, I really want to keep a log of how much time in total theyve spent on the forum though.
Posted: Sun Oct 03, 2004 4:49 pm
by feyd
my way does exactly that, with a lot less work.
Posted: Sun Oct 03, 2004 4:55 pm
by tony montana
So say I log in, view a page - it then updates the database?
What If I view a new page? It updates with the new time right?
So what if I login, leave the site, come back 4 hours later? It counts 4 hours?
Also how would it update correctly - you'd lose previous login hours.
Posted: Sun Oct 03, 2004 5:08 pm
by feyd
the session remains available for a maximum time of, roughly 2 hrs without activity. If enough time passes, your session is deleted, even if you are the only person to come on between the times. You are counted as active, if you have accessed a page within the last, say 5 minutes.
Posted: Mon Oct 04, 2004 5:18 pm
by tony montana
Final function that works ace;
Code: Select all
function clock_timeonline()
{global $user_properties, $forum_properties, $time, $UserDB;
if(($user_properties['loggedIn'] == 1) && ($forum_properties['clock_timeonline'] == 1))
{if(!$_COOKIE['clock_timeonline'] || (($time - $_COOKIE['clock_timeonline']) >= ($forum_properties['clock_timediffo'] + 50)))
{setcookie ('clock_timeonline', $time, time()+(44442000*864000), '/',"".$forum_properties['cookieURL']."", 0);}
elseif($_COOKIE['clock_timeonline'] <= ($time + $forum_properties['clock_timediffo'])#if the time plus diffrence is below or equal to the cookie visit
&& (($time - $_COOKIE['clock_timeonline']) <= ($forum_properties['clock_timediffo'] + 50)))#if the diffrence plus 50 seconds is greater than the cookie visit
{$var = ($time - $_COOKIE['clock_timeonline']);#testing purposes
query("UPDATE $UserDB.unz_users SET timeOnline = timeOnline + $var WHERE `uid` = '".$user_properties['uid']."'") or die(mysql_error());
setcookie ('clock_timeonline', $time, time()+(44442000*864000), '/',"".$forum_properties['cookieURL']."", 0);}}}
Posted: Mon Oct 04, 2004 5:21 pm
by feyd
I'd suggest working on your code style if you ever want to distribute your code

Posted: Tue Oct 05, 2004 11:32 am
by tony montana
Why so
Posted: Tue Oct 05, 2004 11:46 am
by patrikG
tony montana wrote:Why so
It's a wee bit hard to read. Try breaking the code into logical units, with comments preceeding the commented bit of code.
The way I do it:
Code: Select all
////////////
/*
Name: function fetch_directory_entries()
Parameters: none
Description: fetches multiple id and organisation_name from DB
Returns: array
Author: patrikG
Date: 21.08.2004
*/
///////////
function fetch_directory_entries(){
$result = $this->db->query("SELECT id, organisation_name FROM refugee_online_directory ORDER BY organisation_name ASC");
for ($it =& new QueryIterator($result); $it->isValid(); $it->next()){
$record[] = $it->getCurrent();
}
return $record;
}
It's clear what happens, you mimise the time someone needs to understand what's happening (even you after 6 months not looking at your code).
Anyway, this article wraps it up quite well:
http://www.sitepoint.com/article/coding-standards
Also, I would be
very careful with defining globals. Defining globals from within a function is generally considered to be bad practise, because globals can be changed anywhere and tracking down which function changed what is tremendously hideous, time-consuming and at worst can make your code unmaintainable.
Be clear about variable scope - what do you need where. Everything else does not need to be there and complicates things. Define local variables locally, global variables globally (see
http://www.php.net/manual/en/language.v ... .scope.php for different variable scopes). By that I mean that a part from PHP's superglobals ($_SESSION etc.) globals really only make sense as general configuration-constants in a seperate config-file (although most people have started using XML for that). Otherwise globals overcomplicate things.