Page 1 of 1

Users Online

Posted: Tue Jun 19, 2007 12:13 am
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

Posted: Tue Jun 19, 2007 12:20 am
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.

Posted: Tue Jun 19, 2007 12:54 am
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

Posted: Tue Jun 19, 2007 6:04 am
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).