This snippet will show the number of guests and web users online.
It will also list the webusers online - username, IP, time, and
pagetitle(v01b) are available
Set the few variables in the top of the snippet to your liking.
Any requests for features are gladly accepted, just add them here and I will do what I can to accomodate.
note: if you already have the whosonline table created, just run the ALTER statement in the changelog section to update your database (or drop it and recreate it using the sql)
UPDATED CODE: v01b - 5/31/07 2:51 am est
<?php
/* WhosOnline v.01b snippet (initial rough draft working version)
last edit: 5/31/07 2:51 am est
Installation:
- run the following Create Table sql from phpmyadmin to
create the whosonline table, modifying the
<Your table prefix> area to match your database setup.
CREATE TABLE `<Your table prefix>whosonline` (
`username` varchar(32) NOT NULL default '',
`ip` varchar(15) NOT NULL default '',
`time` bigint(10) NOT NULL default '0',
`pageid` varchar(255) NOT NULL default '0',
KEY `username` (`username`),
KEY `ip` (`ip`),
KEY `time` (`time`)
) TYPE=MyISAM PACK_KEYS=0;
Usage:
[!WhosOnline!]
To Do:
- create an installer
- optional snippet params
- set placeholders for display
Changelog:
// 5/31/07 2:41 am est
- added pagetitle info to show where members are located on site
[+onlineuser_onpage+]
ALTER TABLE `<Your table prefix>whosonline` ADD `pageid` VARCHAR( 255 ) DEFAULT '0' NOT NULL ;
*/
// grab the current page info
$page_info =$modx->getPageInfo($modx->documentIdentifier);
//$pinfo_id = $page_info['id'];
$pinfo_ptitle = $page_info['pagetitle'];
// set some text for the display
$guest_pretext_single ="is";
$guest_pretext_multi ="are";
$guest_text_single ="guest";
$guest_text_multi ="guests";
$member_text_single ="member";
$member_text_multi ="members";
// users online - There (is/are) XX (guest/guests) and XX (member/members) online
$default_onlinechunk ="<span class='blogpagination_current'>
There [+guestpretext+] [+guestcount+] [+guesttext+] and [+membercount+] [+membertext+] online<br />
</span>";
// members online
$show_member_list ="yes";
// 5/31/07 added [+onlineuser_onpage+] (pagetitle) to members attributes, this example shows it using hovertext
$default_memonlinechunk ="<span class='blogpagination_current'>
<a href='#' class='blogpagination' title='browsing page: [+onlineuser_onpage+]'>[+onlineusername+]</a> - [+onlineusertime+]<br />
</span>"; // [+onlineuserip+]
// this example prints the page after the name
//$default_memonlinechunk ="[+onlineusername+] - [+onlineuser_onpage+]: [+onlineusertime+]<br />";
$format ="%H:%M:%S";
//$format ="%B %e, %Y - %H:%M:%S";
// http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html
// http://us2.php.net/strftime
////////////////////////////////////////////////////////////////////////
// set up the table
$whotbl = $modx->dbConfig['dbase'].".".$modx->dbConfig['table_prefix']."whosonline";
$ms = mysql_query;
$mf = mysql_fetch_array;
$mr = mysql_num_rows;
// add and remove from the online table
if(getenv('HTTP_CLIENT_IP')) {
$wolip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR')) {
$wolip = getenv('HTTP_X_FORWARDED_FOR');
} else {
$wolip = getenv('REMOTE_ADDR');
}
$newtime = time() - 300;
if(isset($_SESSION['webShortname'])){
$whoonlineuser = $_SESSION['webShortname'];
}else{
$whoonlineuser = "Guest999";
}
//$sql = $ms("DELETE FROM ".$whotbl." WHERE (ip='$wolip') OR (username='$whoonlineuser') OR time<'$newtime'")or die ("Query failed1:<br>$sql<br>Error: " . mysql_error());
$sql = $ms("DELETE FROM ".$whotbl." WHERE (ip='$wolip') OR time<'$newtime'")or die ("Query failed1:<br>$sql<br>Error: " . mysql_error());
$sql = $ms("INSERT INTO ".$whotbl." VALUES('$whoonlineuser', '$wolip', ".time().", '$pinfo_ptitle')")or die ("Query failed2:<br>$sql<br>Error: " . mysql_error());
// retrieve the records
$cguests = $ms("SELECT username FROM ".$whotbl." where username = 'Guest999'");
$guestonline = $mr($cguests);
$cmembers = $ms("SELECT * FROM ".$whotbl." where username !='Guest999'");
$memsonline = $mr($cmembers);
// set the text according to how many users are online
if($guestonline != 1){$guest_pretext =$guest_pretext_multi; $guest_text =$guest_text_multi;
}else{$guest_pretext =$guest_pretext_single; $guest_text =$guest_text_single;}
if($memsonline != 1){$member_text =$member_text_multi;}else{$member_text =$member_text_single;}
//======= start the output
$output ="";
// users online count
$modx->setPlaceholder('guestpretext', $guest_pretext);
$modx->setPlaceholder('guestcount', $guestonline);
$modx->setPlaceholder('guesttext', $guest_text);
$modx->setPlaceholder('membercount', $memsonline);
$modx->setPlaceholder('membertext', $member_text);
$output .=$default_onlinechunk;
// members online list
if($show_member_list =="yes"){
while ($whosonline = $mf($cmembers)) {
$modx->setPlaceholder('onlineusername', $whosonline['username']);
$modx->setPlaceholder('onlineuserip',$whosonline['ip']);
$modx->setPlaceholder('onlineusertime', strftime($format,$whosonline['time']));
// 5/31/07
$modx->setPlaceholder('onlineuser_onpage',$whosonline['pageid']);
$output .=$default_memonlinechunk;
} // end while loop
} // end member list
return $output;
?>
Older versions are attached as text files at the bottom of the post