Who’s Online for Wordpress
After rummaging through the search engine looking for a decent wordpress solution I came across an interesting article about just a simple solution. Well I took that and made it a bit more custom to my need and also included some wordpress features
- Tracks users from the same ip address with user-agent info
- Tracks anyone on your server, not just logged in
- Customizable color background
- Uses remote script run via img src, so no need to worry about install
This script is so incredibly small I can give it out right here.
<?php
define(’DBA_USER’,”);
define(’DBA_PASS’,”);
define(’DBA_HOST’,”);
define(’DBA_NAME’,”);
mysql_connect(DBA_HOST,DBA_USER,DBA_PASS) or die(mysql_error());
if(!mysql_select_db(DBA_NAME)){
echo ‘Unable to select database.’.mysql_error();
exit;
}
$ipaddress = $_SERVER['REMOTE_ADDR'];
$useragent = $_SERVER['HTTP_USER_AGENT'];
$loc = mysql_escape_string(urldecode($_GET['s']));
# Timeout - how long should it take before visitors are no longer ‘online’? (in seconds)
define (’TIMEOUT’, 60*5);
// Make sure image is sent to the browser immediately
ob_implicit_flush(TRUE);
// keep running after browser closes connection
@ignore_user_abort(true);
$inactive = time()-(TIMEOUT);
$countuser = mysql_result(mysql_query(”SELECT count(id) FROM users_online “),0,0);
$NewImage =imagecreatefromjpeg(”img.jpg”);
$LineColor = imagecolorallocate($NewImage,233,239,239);
$TextColor = imagecolorallocate($NewImage, 255, 255, 255);
imagestring($NewImage,5, 5, 2, ($countuser).’ users’, $TextColor);
header(”Content-type: image/jpeg”);
imagejpeg($NewImage);
header(’Connection: Close’);
// Check if visitor is already in the table
$lastactive = time();
$intable = @mysql_result(mysql_query (”SELECT id FROM users_online WHERE server = ‘$ipaddress’ AND useragent = ‘$useragent’”),0,0);
if ($intable == false) {
// Insert new visitor
mysql_query (”INSERT INTO users_online (server, useragent,loc,lastpost) VALUES (’$ipaddress’, ‘$useragent’,'$loc’,$lastactive)”);
} else {
// Update exisiting visitor
mysql_query (”UPDATE users_online SET lastpost = $lastactive, loc = ‘$loc’ WHERE server = ‘$ipaddress’ AND useragent=’$useragent’”);
}
// Remove any inactive visitors
mysql_query (”DELETE FROM users_online WHERE lastpost < $inactive”);
?>
Simple, then just use this database sql
CREATE TABLE `users_online` (
`id` int(11) NOT NULL auto_increment,
`server` varchar(36) NOT NULL,
`useragent` varchar(255) NOT NULL,
`loc` varchar(255) NOT NULL,
`lastpost` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ;
To install it is as follows
- Take the sql supplied above and import into your mysql database
- Copy the php code above into file counter.php
- Configure the database, user login, password, and host
- Then wherever you wish to show and also track your users add the following tag <img src=”//yourhost/counter.php”> Where //yourhost/ is the location of your file.
That’s it, the img will display the amount of users currently on the system you can optionally also set the timeout as in the define.
No Comments yet »
RSS feed for comments on this post. TrackBack URI
Leave a comment
You must be logged in to post a comment.















