We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36720
    • 20 Posts
    Hi All,

    is there any snippet to show users currently online (or active recently) like we have in the Modx Forum right column ?

    Many thanks in advance

    gianni
      • 3749
      • 24,544 Posts
      What version of MODX?
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 36591
        • 38 Posts
        Same question here: I'm using Modx Revolution 2.1.3 pl
          • 3749
          • 24,544 Posts
          I don't think there's any way to get a totally accurate list. This snippet should list active users who have visited a page in the last X minutes:

          [[!ListUsers? &minutes=`15`]]

          <?php
          /* ListUsers Snippet */
          $minutes = $modx->getOption('minutes', $scriptProperties, 20);
          $timetocheck = (time()-(60 * $minutes)) + $serverOffset;
          $c = $modx->newQuery('modActiveUser');
          $c->where(array('lasthit:>' => $timetocheck));
          $c->sortby($modx->getSelectColumns('modActiveUser','modActiveUser','',array('username')),'ASC');
          $ausers = $modx->getCollection('modActiveUser',$c);
          
          $output = "<h3>Current Users</h3>\n<ul>";
          
          foreach($ausers as $auser) {
             $output .= "\n    <li>" . $auser->get('username') . '</li>';
          }
          
          $output .= "\n</ul>";
          
          return $output;
          [ed. note: BobRay last edited this post 12 years, 7 months ago.]
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 36591
            • 38 Posts
            Hi BobRay,

            thanks for that snippet smiley But there seems to be a Parse error: syntax error, unexpected T_VARIABLE on line 8.

            Another method would be to use "thisLogin" - UNIX time stamp (http://rtfm.modx.com/display/revolution20/Users). It's quite usefull although "thisLogin" can't be filtered with strtotime. So the only thing the snippet should do is:


            • Check all "thisLogin" entries
            • Take all the users in the interval (Current time - thislogin)
            • Build a list with the usernames/fullnames
            [ed. note: waivor last edited this post 12 years, 7 months ago.]
              • 3749
              • 24,544 Posts
              Line 3 was missing a comma and line 13 was missing the final semicolon. Fixed above.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 36591
                • 38 Posts
                Hi BobRay,

                Everything works really well. I tested this snippet ([[!ListUsers? &minutes=`15`]]) with two accounts but the snippet doesn't display the usernames at any time. So when does the snippet excatly display a username on the page the snippet is called?
                  • 3749
                  • 24,544 Posts
                  Are you sure there are users online who are logged in in the front end?

                  Try setting &minutes=`1000` and see if anything comes up.

                  I haven't tested it, but most of the code comes from the core code for showing online users in the Manager, so it should work.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 36591
                    • 38 Posts
                    Yes, I am. I created a account and logged in in the front end with the "login" snippet. I changed the minutes option to 1000 but nothing comes up. That's strange.
                      • 3749
                      • 24,544 Posts
                      Hmmm ... Maybe the lasthit field is only updated when the user is in the Manager.

                      I think this snippet would do it, but it might take some time if you have a lot of users:

                      <?php
                      $allUsers = $modx->getCollection('modUser');
                      
                      $count = 0;
                      
                      foreach ($allUsers as $aUser) {
                         if ($aUser->hasSessionContext('web') ) {
                             $output .= "\n    <li>" . $aUser->get('userName') . '</li>';
                             $count++;
                         }
                      }
                      
                      if ($count) {
                         return "\n<ul>" . $output . "\n</ul>";
                      } else {
                         return '<p>No Users online</p>';
                      }
                      

                      You could also do it (much faster) with a plugin tied to OnWebLogin and OnWebLogout that stored the username (and maybe the time) in a custom DB table on login and deleted it on logout. I don't know if OnWebLogout catches users who just close the browser, though, so you might have to delete old entries before getting the current list.

                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting