Home » Tutorials

PHP Users Online with Mysql

Using PHP and Mysql you can keep track of the users online your website and display it on your page.

First thing ,we need a table in our Mysql database for the users online. So let’s make a table in our database.

mysql_query("CREATE TABLE online(
id INT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
ip_address VARCHAR(25) NOT NULL,
timestamp VARCHAR(25) NOT NULL)") or die(mysql_error());

Now that we have the table, we need to update the users online on every page. That way it is most accurate and up to date with who is currently online. We will set a time offset so that we include all users online within the past 5 minutes.

<?php

// We need to get the user's ip address to check in the database. We'll make a function called ipaddress().
function ipaddress(){
if(getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif(getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif(getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif(getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif(getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}

$time_offset = time() - (60*5);
$ip = ipaddress();
$sql = mysql_query("SELECT id FROM online WHERE ip_address = '$ip' LIMIT 1") or die(mysql_error());

if(mysql_num_rows($sql) > 0){
mysql_query("UPDATE online SET timestamp = '".time()."' WHERE ip_address = '$ip') or die(mysql_error());
} else {
mysql_query("INSERT INTO online (ip_address,timestamp) VALUES ('$ip','".time()."')") or die(mysql_error());
}
$delete = mysql_query("DELETE FROM online WHERE timestamp < '$time_offset'") or die(mysql_error());

?>

Now every time someone goes on a page with this code, mysql will check to see if the user is in the system, and if so update their current timestamp, otherwise create a row for them in the table. It will also erase any rows with old timestamps. This will keep the table clean and updated.

Now we count the users online and show it on the page.

<?php

function online(){
$time_offset = time() - (60*5);
$sql = mysql_query("SELECT * FROM online WHERE timestamp > '$time_offset'");
$online = mysql_num_rows($sql);

return 'There are '.$online.' user(s) currently online. ';
}
?>
0

Popularity: 3% [?]

Share/Bookmark this!

4 Comments

Leave a reply

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

Side Notes

This entry was posted by on November 7, 2009 at 7:25 PM and filed under Tutorials category.

You can add your comments or trackback from your own site. To keep you updated to the latest discussion, you can subscribe to these comments via RSS.

Recent Entries

Pages

Recent Comments

Resources

Questions & Answers

Just started! Have not answered any questions.

Tag Cloud

background body CSS database date dropdown email error_reporting favicon file filesize form format function global hosting HTML image Javascript limit link linkbar mysql numbers online option ordinalize pagination pattern photoshop PHP resize rows script search snowing stylesheet switch table thumbnail time timestamp upload validate variables

Sponsors