Users Online

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
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Users Online

Post by bob_the _builder »

Hi

I have made a script to show how many people on line and the most users at one time:

Code: Select all

<?php

	putenv("TZ=Pacific/Auckland");
	$insertdate = date('Y-m-d H:i:s');
	$limit_time = time() - 300;

	if(!session_is_registered('online')){ 
    mysql_query("INSERT INTO ppl_online (session_id, activity, ip_address, refurl, user_agent) VALUES ('".session_id()."', '$insertdate', '".$_SERVER['REMOTE_ADDR']."', '".$_SERVER['HTTP_REFERER']."', '".$_SERVER['HTTP_USER_AGENT']."')"); 
    session_register('online'); 
	}
 
	if(session_is_registered('online')){         
    mysql_query("UPDATE ppl_online SET activity='$insertdate' WHERE session_id='".session_id()."'"); 
	}

	$inactive = time() - 1800;
	mysql_query ("DELETE FROM ppl_online WHERE UNIX_TIMESTAMP(activity) < $inactive");

	$total = mysql_query("SELECT * FROM ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time GROUP BY ip_address"); 
	$totalonline = mysql_num_rows($total);

	$most = mysql_query("SELECT * FROM most_users"); 
	while($row = mysql_fetch_array($most)){
	$most_users = $row['total'];
	$most_date = date('D dS M Y, h:i a', strtotime($row['date']));
	}

	if ($totalonline > $most_users) {
	$sql = mysql_query("UPDATE most_users SET total='$totalonline', date='$insertdate' WHERE id=1");
	}

?>
Since adding:

Code: Select all

putenv("TZ=Pacific/Auckland");
$insertdate = date('Y-m-d H:i:s');
To the script it seems to total all the users that have been online even it it was 10 minutes ago. How can I fix this?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You may want to use date() instead of time() results. It shouldn't make too much of a difference, but it can since you're mixing both.

Also, session_is_registered() and session_register() should no longer be used. $_SESSION should be interacted with instead using isset() or similar.
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

Cleaned up the redundant code. If I use date() instead of time() it generates an error?

What replaces session_register('online'); is it:

$online = 'online';
$_SESSION['online'];


Thanks
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Sessions are no longer governed by session functions. They are a superglobal array, and can be used as such.

Code: Select all

$_SESSION['online'] = 'online';
Treat it like $_POST and $_GET, except you can set the values as well (and not have them disappear).
Post Reply