Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
CREATE TABLE users_online (
timestamp int(15) NOT NULL default '0',
ip varchar(40) NOT NULL default ',
PRIMARY KEY (ip)
) TYPE=MyISAM;
// returns number of users currently on the site
function getUsersOnline() {
# Database-specific information:
$server = "localhost";
$database = "db_name";
$user = "user_name";
$pw = "password";
# connect to the database
$connection = @mysql_connect ($server, $user, $pw) or die ("Error connecting to the database.");
# select the database
$db = @mysql_select_db ($database) or die ("Error selecting database.");
# time limit for users online in seconds - you can increase or decrease this if you want
# recommended: entries in db older than 1 minute will be removed
$timeout = 60;
# get the ip address
$ip = getenv("REMOTE_ADDR");
# get the time
$timestamp = time();
$timelimit = $timestamp - $timeout;
# delete expired users
$sql = "DELETE FROM users_online WHERE timestamp < $timelimit";
$result = @mysql_query ($sql);
# insert current user
$sql = "INSERT INTO users_online (timestamp, ip) VALUES ('$timestamp', '$ip')";
$result = mysql_query ($sql);
# if ip already exists, update the timestamp
if (mysql_errno() == 1062) {
$sql = "UPDATE users_online SET timestamp = '$timestamp' WHERE ip = '$ip'";
$result = mysql_query ($sql);
}
# get number of users online
$sql = "SELECT COUNT(*) as num_users FROM users_online";
$result = mysql_query ($sql);
$row = mysql_fetch_array($result);
$num_users = $row['num_users'];
if ($num_users == 1)
return "1 user";
else
return $num_users . " users";
}
<? echo getUsersOnline(); ?>