Jump to content

Script for who is online at my site


ghgarcia

Recommended Posts

Well, you'll need a database named 'online' with 2 rows (fields 'ip' (TEXT), and 'timestamp' (INT)).


Note this may not be the best way to do this but this is how i would do it.
[code]
<?php
//connect to you database
mysql_connect("localhost", "user", "pass");
mysql_select_db("your_db");

//get the users ip
$user_ip = $_SERVER['REMOTE_ADDR'];
//get the current time
$time = time();

//add 300 seconds (5 mins) to the time variable
$stamp = $time + 300;

//query for previous entries frm this user
$query = mysql_query("SELECT * FROM 'online' WHERE 'timestamp' > '$time' AND 'ip' = '$user_ip'");
if(!mysql_num_rows($query))
{
//if no entries are found, add one
mysql_query("INSERT INTO 'online' ('ip', 'timestamp') VALUES('$user_ip', '$stamp')");
}
else
{
//if one is found update the time stamp
mysql_query("UPDATE 'online' SET 'timestamp' = '$stamp' WHERE 'ip' = '$user_ip'");
}
//delete idle entries
mysql_query("DELETE FROM 'online' WHERE 'timestamp' < '$time'");

//pull out all entries
$query = mysql_query("SELECT * FROM 'online'");

//get the number of entres
$num_users = mysql_num_rows($query);

//display it
echo $num_users;
?>
[/code]

that should work, will be accurate within 5 mins.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.