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