usersonline tutorial
Posted: Tue Sep 23, 2003 9:35 am
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. 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.
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
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: again, u can use this function in as many pages as you like. Call it like so: 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
call this function so:
__________________________________________
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
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;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');
}
?>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:
Code: Select all
<?php
function show_usersonline()
{
$check = mysql_num_rows(mysql_query("SELECT DISTINCT ip FROM `useronline`"));
echo $check.” Users online”;
}
?>Code: Select all
<?php
show_usersonline();
?>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;
}
?>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