scriptygoddess

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']) : "";
}

16 Responses to "Number of users currently online (part II)"

1 | Lynda

March 27th, 2004 at 8:00 am

Avatar

FYI, I made the script go from a flat file instead of a SQL table because of the constant in and out of information. Tables aren't really meant to have info deleted and entered every few seconds, so a flat file is really a better place to put this sort of info.

2 | Lynda

March 27th, 2004 at 8:51 am

Avatar

I hope this counts as a suggestion, not a problem. Feel free to delete this comment as needed after it's seen.

I don't see the above checking to make sure the users isn't already in the table before adding it. It seems like if the same users is there and gets different timestamps, it will count as more than one user. This is partially why I included the email information because it was easiest to exclude like email addresses and THEN exclude like IPs for anonymous users.

3 | Jennifer

March 27th, 2004 at 8:56 am

Avatar

Yeah, I did change that… reason is – I know many people who don't post with email addresses. So it should be selecting unique people who have the same name, ip and URL. Would probably be better if I had all four… last night at midnight it made sense. LOL!

And yes – it does a new row each time you hit/refresh a page. It deletes those when the timestamp expires.

4 | Jennifer

March 27th, 2004 at 9:06 am

Avatar

I should add – a lot of how that works (what it adds to the database, etc.) is from the original Spoono script… I just added your MT cookie grabbing to it.

5 | Lynda

March 27th, 2004 at 10:34 am

Avatar

Why not use IP then to prevent duplicate entries? It seems more accurate than counting another person every time they hit refresh or go to another page.

6 | Jennifer

March 27th, 2004 at 3:14 pm

Avatar

Well, I'd still have to check something more than just IP – for the case of two+ people at the same IP…

It doesn't count another person each time they hit refersh… it's selecting unique entries… so if there's 5 entries for me (my combination of name/url/ip) – it only counts it as "one user online".

7 | Jenni

March 30th, 2004 at 6:41 pm

Avatar

Jenn, thanks so much for the fixes! The script is great. And thanks for offering to help me with my non-existent problem, too. 😉

Sorry I pinged you (and Lynda) twice on this topic. It was TrackBack autodiscovery's fault. :(

8 | Bloggie Broad

March 30th, 2004 at 5:27 pm

Avatar

Online Users' Script Fix from Jenn
Thanks, Jenn! The MT online users' script from Scriptygoddess has…

9 | Bloggie Broad

March 30th, 2004 at 6:27 pm

Avatar

Scripty's Online Users' Script Updated
Thanks, Jenn! The MT online users' script from Scriptygoddess has…

10 | Code Novice

April 15th, 2004 at 12:56 am

Avatar

Scripty's Online Users Script
The MT online users' script from Scriptygoddess is now available….

11 | Faithjean.com :: Freak of nature ::

April 16th, 2004 at 7:03 pm

Avatar

2 Do list
1. Misc templates (Trackback, search result, comment listing…) needs skinning 2. Implement this code in the comment listing template 3. Master Archive: a) Posts by quotations b) Posts by citations c) Posts by b links 3. Function "Change skins.php" 4….

12 | Sara

July 6th, 2004 at 11:35 am

Avatar

Thank you for this, Jenn! :)

13 | Kamran

July 22nd, 2004 at 2:29 am

Avatar

But aren't these kind of users online scripts depanding upon user's machine time(), if users are in different time zones, what will happen?

14 | Jennifer

July 22nd, 2004 at 7:22 am

Avatar

Actually it's dependent on the SERVER's time(), not users – since this is a SERVER SIDE PHP script. So someone hitting it from a different timezone will not make a difference. (I think you might be thinking of script that uses JAVASCRIPT to get the time – in which case it would be dependent on the users time – since Javascript is a CLIENT SIDE script). In any case that is not the case here.

15 | Tek

November 19th, 2004 at 4:36 pm

Avatar

So how do I include it in the page? What is the php I need to call the info?

16 | Tek

November 19th, 2004 at 8:41 pm

Avatar

I figured out how to get this to work and now it is!!

Woot!

Have I told you how much I love you lately? 😉

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements