27 Mar, 2004
Number of users currently online (part II)
Posted by: Jennifer In: MT scripts|WordPress scripts
I've been looking around for "users currently online" type scripts. So, tonight, I hacked together two great scripts. The first comes from Spoono – this script uses php and mySQL to keep track of how many users are on your site. Then tonight I saw Lynda's script – which uses php, a flat file and makes use of MT's cookies to actually display WHO is on your site (they would have had to leave a comment on the site to show up in this list)
So I combined the two…
(IMPORTANT NOTE: I'm still working out the kinks with this script. If you use this and have issues – please email me (scripty aT scriptygoddess dOt nEt) – don't leave a comment… that way the comments section doesn't get out of hand… I'd prefer to keep the comments section reversed for either modifications/alternate hacks or additional suggestions)
Here is the modified "create table" script
CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
userName varchar(50) NOT NULL,
userURL varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);
The way the Spoono's original script worked was that it would (I believe) only tell you how many users were on a particular page – but the way my script below works is that it shows users on the entire site…
On Lynda's site, she pointed out that you need to modify the "rememberme" and "forgetme" javascript functions. See her site for the details.
Now here's the updated script. (I made this a seperate file and put it on my site as an include where I wanted to the "users on site" to display)
<?php
//fill in some basic info
$dbsvr= "localhost";
$dbusrnam = "your-database-username";
$dbpswrd = "your-database-password";
$database = "your-database-name";
$timeoutseconds = 300;
$mt_name = isset($_COOKIE['mtcmtauth']) ? ($_COOKIE['mtcmtauth']) : "";
$mt_url = isset($_COOKIE['mtcmthome']) ? ($_COOKIE['mtcmthome']) : "";
$mt_ip = getenv("HTTP_X_FORWARDED_FOR") ? getenv("HTTP_X_FORWARDED_FOR") : getenv("REMOTE_ADDR");
// If someone doesn't want their name to appear on the list, enter their
// urls below, separated by a comma. Example:
// $blocked_urls = "http://www.domain1.com, http://www.domain2.com";
$blocked_urls = "";
// If user is in blocked report above, make them anonymous and then add to file
if ($blocked_urls !="") {
$blocked = explode(",", $blocked_urls);
for ($i=0; $i < count($blocked); $i++) {
if (trim($blocked[$i]) == $mt_url) {
$mt_name = "";
$mt_url = "";
}
}
}
//get the time
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;
//connect to database
mysql_connect($dbsvr, $dbusrnam, $dbpswrd);
//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','$mt_ip ','','$mt_name','$mt_url');");
if(!($insert)) {
print "Useronline Insert Failed > ";
}
//delete values when they leave
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp < $timeout");
if(!($delete)) {
print "Useronline Delete Failed > ";
}
//grab the results
$result = mysql_db_query($database, "SELECT DISTINCT ip, userName, userURL FROM useronline");
if(!($result)) {
print "Useronline Select Error > ";
}
//number of rows = the number of people online
$user = mysql_num_rows($result);
$display = "";
$annon = 0;
mysql_close();
for ($i = 0; $i < $user; $i++) {
$row= mysql_fetch_array($result);
if ($row["userName"] != "" && $row["userURL"] != "") {
//make sure url has "http://" in it
$isHttpUrl = strpos($row["userURL"], "http://");
if ($isHttpUrl === false) {
$row["userURL"] = "http://".$row["userURL"];
}
$display .= "<a href=\"".$row['userURL']."\">".$row["userName"] ."</a><br />";
} else if ($row["userName"] != "") {
$display .= $row["userName"] ."<br />";
} else {
$annon++;
}
}
if ($display != "") {
echo $display;
}
if ($annon != "") {
echo $annon;
if ($annon <= 1) {
echo " user";
} else {
echo " users";
}
}
?>
To use this script with wordpress, change these two lines in the script above:
$mt_name = isset($_COOKIE['mtcmtauth']) ? ($_COOKIE['mtcmtauth']) : "";
$mt_url = isset($_COOKIE['mtcmthome']) ? ($_COOKIE['mtcmthome']) : "";
To this:
$mt_name = (isset($_COOKIE['comment_author_'.$cookiehash])) ? trim($_COOKIE['comment_author_'.$cookiehash]) : '';
$mt_url = (isset($_COOKIE['comment_author_url_'.$cookiehash])) ? trim($_COOKIE['comment_author_url_'.$cookiehash]) : '';
(yes, I know the variables still say "mt" in them – if that really confuses you, just do a find and replace and change them.
If you have MOVED OVER from MT to WP – and still want to capture the names of people who may or may not have left a comment on your wordpress blog yet (so they don't have cookies from wp comments – but probably still have MT cookies), change those lines to this instead:
$mt_name = (isset($_COOKIE['comment_author_'.$cookiehash])) ? trim($_COOKIE['comment_author_'.$cookiehash]) : '';
if ($mt_name == '') {
$mt_name = isset($_COOKIE['mtcmtauth']) ? ($_COOKIE['mtcmtauth']) : "";
}
$mt_url = (isset($_COOKIE['comment_author_url_'.$cookiehash])) ? trim($_COOKIE['comment_author_url_'.$cookiehash]) : '';
if ($mt_url == '') {
$mt_url = isset($_COOKIE['mtcmthome']) ? ($_COOKIE['mtcmthome']) : "";
}