[SOLVED] Counting how long a users been online, code done; l

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

[SOLVED] Counting how long a users been online, code done; l

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I think you should seriously rethink how you organize your code.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

my way does exactly that, with a lot less work.
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post 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);}}}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd suggest working on your code style if you ever want to distribute your code ;)
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post by tony montana »

Why so
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
Post Reply