We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4041
    • 788 Posts
    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 smiley
      xforum
      http://frsbuilders.net (under construction) forum for evolution
      • 20289
      • 958 Posts
      great job... keep up the good work, a kind of an option for history review of latest visitors (essentially members) online within the predefined period of time would be nice to implement...
        [img]http://i10.tinypic.com/52c4eir.gif[/img][/td]
        [td][Wiki] [Persian support forum]
        [SVN] [RTL SVN Branch] [bugs] [FishEye+Crucible] [Learn MODx!] | [My Google Code]
        [font=tahoma][برای دسترسی به راهنمای فارسی به [url=http://www.modxcms.ir]
        • 4041
        • 788 Posts
        I put an updated version in the top post (v.01a) which has a few text and format options.
        5/30/07 3:36 am est

        Give it a try and let me know how it goes and keep improvement suggestions coming smiley
          xforum
          http://frsbuilders.net (under construction) forum for evolution
          • 17282
          • 283 Posts
          I get this :

          Query failed1:

          Error: DELETE command denied to user ’urtzhjz67_batmflop’@’localhost’ for table ’modx_whosonline’

          any suggestions?
            Everytime you use Flash ... a puppy dies .....
            R.G Taylor
            • 17282
            • 283 Posts
            ahh wait!

            its conflicting with the SMS SSI stuff.. ive had that a lot ..
              Everytime you use Flash ... a puppy dies .....
              R.G Taylor
              • 4041
              • 788 Posts
              sounds like the user doesnt have the correct database priviledges to me but thats just a guess... smiley
                xforum
                http://frsbuilders.net (under construction) forum for evolution
                • 25317
                • 122 Posts
                I’m using this snippet and it works a treat. Great work Breezer! grin
                  • 20289
                  • 958 Posts
                  works fine as expected wink ... whats next?
                    [img]http://i10.tinypic.com/52c4eir.gif[/img][/td]
                    [td][Wiki] [Persian support forum]
                    [SVN] [RTL SVN Branch] [bugs] [FishEye+Crucible] [Learn MODx!] | [My Google Code]
                    [font=tahoma][برای دسترسی به راهنمای فارسی به [url=http://www.modxcms.ir]
                    • 4041
                    • 788 Posts
                    I was thinking of adding the ability to show the manager users online, don’t know how useful that would be though...

                    If this tests out well and no issues arise then I will likely try to improve it small bits at the time so keep an eye out here, and any more suggestions for features are very welcomed smiley
                      xforum
                      http://frsbuilders.net (under construction) forum for evolution
                      • 20289
                      • 958 Posts
                      Quote from: breezer at May 30, 2007, 04:39 PM

                      I was thinking of adding the ability to show the manager users online, don’t know how useful that would be though...
                      Any optional feature would be great... try to post a new reply to notify an update... thanks wink
                        [img]http://i10.tinypic.com/52c4eir.gif[/img][/td]
                        [td][Wiki] [Persian support forum]
                        [SVN] [RTL SVN Branch] [bugs] [FishEye+Crucible] [Learn MODx!] | [My Google Code]
                        [font=tahoma][برای دسترسی به راهنمای فارسی به [url=http://www.modxcms.ir]