Page 1 of 1

usersonline tutorial

Posted: Tue Sep 23, 2003 9:35 am
by qads
there have been quite alot of posts on this matter, so this is a tutorial on how to show number of users online, you just need little
understanding of functions and how to use them. I expect you already have a working database connection.
_______________________________________________________


we are going to make a mysql database table to hold our information.

Code: Select all

CREATE TABLE `useronline` ( `timestamp` varchar(20) NOT NULL default '', `ip` varchar(40) 
NOT NULL default '', `username` varchar(32) NOT NULL default '', PRIMARY KEY (`timestamp`), KEY `ip` (`ip`), KEY `file` (`username`) ) TYPE=MyISAM;
this is the only
table we need, it has 3 fields to hold the information. Now lets make a function to insert data into this table.

Code: Select all

<?php
function usersonline($user)
{
$ip = $_SERVER["REMOTE_ADDR"];
$timeoutseconds = 300; 
$timestamp = time(); 
$timeout = $timestamp-$timeoutseconds;
$insert = mysql_query("INSERT INTO useronline VALUES ('$timestamp','$ip','$user')"); 
$delete = mysql_query("DELETE FROM useronline WHERE timestamp < '$timeout','$user');
}
?>
the only thing you may want to change: $timeoutseconds – set this in number
of seconds, default value is 300 seconds which is 5 minutes This is how long information
about a user on your site will be kept, after the set value, it will be deleted,
if the user is still online then a new record will be inserted with a new timestamp
value. You can use this function in as many pages as you like, just call it like
so

Code: Select all

<?php
usersonline($user);
?>
this can also be used as who's online function with little tweaking, just pass a username in $user var, lets show how many users they are on your site :) We need another function for
this:

Code: Select all

<?php
function show_usersonline()
{
$check = mysql_num_rows(mysql_query("SELECT DISTINCT ip FROM `useronline`"));
echo $check.” Users online”;
}
?>
again, u can use this function in as many pages as you like. Call it like so:

Code: Select all

<?php
show_usersonline();
?>
that’s it, your done. Happy counting :)




PEOPLE WHO WANT SHOW WHO IS ONLINE
make sure you have passed username to the db in above function.

use this function

Code: Select all

<?php
function whoisonline()
{
$query = mysql_query("SELECT DISTINCT username FROM `useronline`");
while($row = mysql_fetch_array($query))
{
$users .= $row[username]."<br />";
}
return $users;
}
?>
call this function so:

Code: Select all

<?php
echo whoisonline();
?>



__________________________________________

This is my first tutorial ever so be gentle, I don’t like to write tutorials cos
I can’t explain very well lol. If you have any problems or questions then post them here
thanks

Posted: Tue Sep 23, 2003 11:00 am
by Toneboy
You can do it with sessions as well, making it a bit more advanced. My own version counts number of pages viewed, along with first page viewed, current page viewed. Good fun learning it and applying it as it suits you. :)

Posted: Tue Sep 23, 2003 11:22 am
by qads
there always another way of doing something, thats one of the best things in php :)

Posted: Tue Sep 23, 2003 3:23 pm
by Aaron
Works nicely, Im testing it on a forum Im coding. It kept coming up at 0 users online so I took a closer look and realise; Your inserting - then deleting' so I switched them round and it works like a charm when theres 1 person on there :D dunno about more.

Posted: Tue Sep 23, 2003 3:24 pm
by Aaron
always says one person online ;/

Posted: Tue Sep 23, 2003 3:40 pm
by Aaron
edited it slightly to get it working

Code: Select all

function usersonline($uid) 
	{$ip = $_SERVER["REMOTE_ADDR"]; 
	$time = time();
	$timeoutseconds = '300'; 
	$timeout = $time - $timeoutseconds; 
	$delete = mysql_query("DELETE FROM unz_online WHERE time < '$timeout'");
	$insert = mysql_query("INSERT INTO unz_online VALUES ('$time','$ip','$uid')");} 

	function show_usersonline() 
	{$check = mysql_num_rows(mysql_query("SELECT DISTINCT ip FROM `unz_online`")); 
	echo "$check Users online";}

Posted: Tue Sep 23, 2003 4:16 pm
by jason
Do you want to edit your original post, and update it appropriately. I will go through, edit it, and move it to the Tutorials forum.

Posted: Tue Sep 23, 2003 5:53 pm
by qads
here you go jason, fixed it and added little more :D

thanks for feedback Aaron.

Posted: Wed Sep 24, 2003 1:17 pm
by Aaron

Code: Select all

function online_user($udb)
	{$Q = mysql_query("SELECT DISTINCT uid FROM `unz_online`");
	while($online = mysql_fetch_object($Q))
	{$check = mysql_query("SELECT username, status, uid FROM $udb.unz_users WHERE uid = '$online->uid'"); 
	$user = mysql_fetch_object($check);
	if($online->status < 2)
	{$user = "<font color='blue'><a href='?section=profile&uid='$user->uid'>$user->username</a></font>";}
	elseif($online->status < 2)
	{$user = "<font color='red'><a href='?section=profile&uid='$user->uid'>$user->username</a></font>";}
	echo "$user";}}