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

    I want to find a way to display the currently logged in users on the page at all times. Is there a snippet that does this already?

    Any help would be greatly appreciated.

    Thankyou smiley

    Sarah
      • 18397
      • 3,250 Posts
      Logged in web users?
        • 26182
        • 164 Posts
        i’ve started writing a plug in for it that sets a placeholder that should correspond to the number of users that are currently online. i just don’t know what database to use that stores the login information. for example in the corner of these forum boards it tells you at all times that you are logged in with a number of other users.

        i was thinking about comparing the current timestamp to the timestamp of the last hit that the user made and if they have not made a hit in the last 10 mintues or so logging them out automatically and taking them off the tally of users that are recorded as being online.

        do you understand what i mean?

        thanks smiley

        sarah
          • 18397
          • 3,250 Posts
          Again, do you want a count of logged in MODx web users?
            • 26182
            • 164 Posts
            no just logged in users on the online community that we are creating. i’ve got this so far:

            //This plugin that checks the database to see how many users are logged in and sets a placeholder that the site's template refers to to display the number of users who are currently viewing the site
            
            $storetime = 0;
            
            if(!isset($timestamp)) {
                $timestamp=time();
            }
            
            $storetime = $timestamp+$modx->config['server_offset_time'];
            
            //select the modx table to use
            $table = $modx->getFullTableName("active_users");
            
            //variable to store the number of users who are logged on
            $loggedIn = 0;
            
            //iterate through the table and see whish users are logged on. Increment the number of users that are logged in if they are logged in
            
            $result = $modx->db->select("*", $table); 
            
            //while there are still more rows in the table check to see if the user is logged in
            
            while($r = modx->db->getRow($result))   
            { 
                 $user = $r["username"];
                 $hit = $r["lasthit"];
                 $tempTimeStore = 0;
            
                 //compare the timestamp of the last hit with the current timestamp and see if the last hit occured in the last 10 minutes. If it did not the user is not currently online
                 
                 $tempTimeStore = $storetime - $hit;
            
                 //if the result is 
                  if ($tempTimeStore <= 600)
                  {
                      //do not increment the number of users logged in
                        $loggedIn++;
            
                  }
                  
                   
            }
               
            
            //Set the placeholder to display the number of users who are logged in
            $modx->setPlaceholder('UsersOnline', "$loggedIn");


            but it doesn’t seem to be setting the placeholder.

            any suggestions?

            sarah

              • 18397
              • 3,250 Posts
              Try it as a snippet with the last line as
              return $loggedIn;
              


              Instead of
              $modx->setPlaceholder('UsersOnline', "$loggedIn");