We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7923
    • 4,213 Posts
    One idea, add a document ID to the database table also and possibility to the snippet to show how many users are currently on the same page.. Good job breezer!


      "He can have a lollipop any time he wants to. That's what it means to be a programmer."
      • 4041
      • 788 Posts
      I have updated the code in the first post (v01b), the pagetitle has been added to the members output...

      there are a couple of examples ($default_memonlinechunk) which show usage, one is a link hover attribute and one just plain output wink

      thanks for all the kind words and keep the suggestions coming....
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 20289
        • 958 Posts
        works fine, just a suggestion:
        - i prefer not to show the page that member is in by "title" attribite, but some string is fine.
        - clock time display of when a particular member signed in is ok but time counter will make better sense...
        ie: Mohsen: 1minute
        ...assume "Mohsen" is the username and "1minute" is the time he has spent to be logged in.

        All the best 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]
          • 30524
          • 22 Posts
          Nice Work!

          I just added some snippet parameters:

          $output : boolean (default: false)
          If false there will be no output

          $show : string (count,members,all)
          count - shows the count
          members - shows the list of members
          all - shows everything

          
          <?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 ;
          
          */
          
          //snippet parameter added traedamatic 220410
          $output = isset($output) ?  true : false;
          
          
          
          $show =  isset($show) ? $show : "all"; //count, members , all
          
          
          
          // 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
          if ($show == "members" || $show == "all") //added traedamatic 220410
                  $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
          if($output) { //added traedamatic 220410
              $output ="";
              
              // users online count
              if ($show == "all" || $show == "count" ) { //added traedamatic 220410
                 
                 $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;
          }
          ?>
          
          
            • 16702 ☆ A M B ☆
            • 536 Posts
            Quote from: traedamatic at Apr 22, 2010, 06:42 AM

            Nice Work!

            I just added some snippet parameters:

            $output : boolean (default: false)
            If false there will be no output

            $show : string (count,members,all)
            count - shows the count
            members - shows the list of members
            all - shows everything

            
            


            hello,

            i test your code, but is not work for me....
            i have no errors in System Events
            my bd is 5.0.77
            PHP Version 5.1.6
            modx is 1.0.3
              palma non sine pulvere
              • 30524
              • 22 Posts
              hey,

              what is your snippet call?

              [!WhoIsOnline? &output=`true` &show=`members`!]  
              


              this one is printing out the members but no count
                • 30524
                • 22 Posts
                I found another bug!

                the $modx->setPlaceholder function parses the placeholder "onReturn". I had the problem that only the last member which logged was displayed but repeated so many times as the amount of logged in users.

                I replaced the setPlaceHolder function with the normal str_replace function and now it works

                
                <?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 ;
                
                */
                
                //snippet parameter added traedamatic 220410
                $output = isset($output) ?  true : false;
                
                
                
                $show =  isset($show) ? $show : "all"; //count, members , all
                
                
                
                // 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
                if ($show == "members" || $show == "all") //added traedamatic 220410
                        $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
                if($output) { //added traedamatic 220410
                    $output ="";
                    
                    // users online count
                    if ($show == "all" || $show == "count" ) { //added traedamatic 220410
                       
                       $output .=$default_onlinechunk;
                       
                       //replaced setPlaceholder with str_replace 270410 traedamatic
                       $output = str_replace('[+guestpretext+]', $guest_pretext,$output);
                       $output = str_replace('[+guestcount+]', $guestonline,$output);
                       $output = str_replace('[+guesttext+]', $guest_text,$output);
                       $output = str_replace('[+membercount+]', $memsonline,$output);
                       $output = str_replace('[+membertext+]', $member_text,$output);
                    
                       
                    
                    }
                    
                    // members online list
                    if($show_member_list =="yes"){
                    
                      while ($whosonline = $mf($cmembers)) {
                
                       $output .=$default_memonlinechunk;
                       $output = str_replace('[+onlineusername+]', $whosonline['username'],$output);
                       $output = str_replace('[+onlineuserip+]',$whosonline['ip'],$output);
                       $output = str_replace('[+onlineusertime+]', strftime($format,$whosonline['time']),$output);
                       // 5/31/07
                       $output = str_replace('[+onlineuser_onpage+]',$whosonline['pageid'],$output);
                    
                
                    
                      } // end while loop
                    } // end member list
                    
                    return $output;
                }
                ?>
                
                  • 16702 ☆ A M B ☆
                  • 536 Posts
                  Quote from: traedamatic at Apr 27, 2010, 11:33 AM

                  hey,

                  what is your snippet call?

                  [!WhoIsOnline? &output=`true` &show=`members`!]  
                  


                  this one is printing out the members but no count

                  i try with this call, and i have no results.
                  strange...i see with phpmyadmin in bd i have records smiley

                  hmmm, today i make some test in another server - maybe problem is in php version or mysql or modx ver. laugh
                    palma non sine pulvere