Forum Moderators: coopster

Message Too Old, No Replies

Help With Online Counter

         

naitsirhc26

5:38 pm on Sep 20, 2006 (gmt 0)

10+ Year Member



Okay, I need help with an online counter. Does anyone have a simple coutner that I could use that displays how many visitors are online? I have this code so far, but I was wondering if you could change the MySQL database, and code, so it only displays how many visitors are online, not registered visitors, etc..Thanks.

MySQL Database:

CREATE TABLE ppl_online (
session_id varchar(255) NOT NULL default '',
activity datetime NOT NULL default '0000-00-00 00:00:00',
member enum('y','n') default 'n',
ip_address varchar(255) NOT NULL default '',
refurl varchar(255) NOT NULL default '',
user_agent varchar(255) default NULL,
PRIMARY KEY (session_id),
KEY session_id (session_id)
) TYPE=MyISAM;


Global Header:

<?php
if(!session_is_registered('online')){
@mysql_query("INSERT INTO ppl_online (session_id, activity, ip_address, refurl, user_agent) <br> VALUES ('".session_id()."', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGENT']}')");
session_register('online');
} else {
if(session_is_registered('user_id')){
@mysql_query("UPDATE ppl_online SET activity=now(), member='y' WHERE session_id='".session_id()."'");
}
}
if(session_is_registered('online')){
@mysql_query("UPDATE ppl_online SET activity=now() WHERE session_id='".session_id()."'");
}
?>

Website Contect, Display Counter Code:

<?php
// This file is included into your website
// Preferably a MySQL connection has been established already

$limit_time = time() - 300; // 5 Minute time out. 60 * 5 = 300
$sql = mysql_query("SELECT * FROM ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='n' GROUP BY ip_address") or die (mysql_error());
$sql_member = mysql_query("SELECT * FROM ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='y' GROUP BY ip_address") or die (mysql_error());
$visits = mysql_num_rows($sql);
$members = mysql_num_rows($sql_member);

echo "People Online:<br />";
echo "Guests Online: $visits<br />";
echo "Members Online: $members<br />";
?>

naitsirhc26

11:43 pm on Sep 20, 2006 (gmt 0)

10+ Year Member



Anyone?

barns101

8:54 am on Sep 21, 2006 (gmt 0)

10+ Year Member



I have never used one, so can't help as such. However, phpBB [phpbb.com] has such a feature and so you could use their code.

dreamcatcher

1:17 pm on Sep 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ty googling for 'users online php', that should get you something. Also, the script repositories like hotscripts.com have a few of these code snippets, so check there.

dc

rokec

7:28 pm on Sep 30, 2006 (gmt 0)

10+ Year Member



I have much easier code for you:

create a new column in your database, named online.

Set this to 1 on logging in and 0 when logging out.

This code should print the number of online users:

echo(mysql_row_count(mysql_query("SELECT * FROM yourdatabase WHERE online='1'"))); //counts the row with online=1 and echo() it.

This code shows users, who are online (session set). For aditional you can create new col named lastact. At the beggining of each document you should update this lastact:

$time=time(); //set current time to $time
mysql_query("UPDATE yourdatabase set lastact='$time'))); //updating lastact...

and then you check how many users are logged in:

$time=time();
echo(mysql_row_count(mysql_query("SELECT * FROM yourdatabase WHERE (online='1') and ('lastact=$time-600'"))); //$time-600 = 10 mins ago

Hope helped you a bit.