We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10329
    • 2 Posts
    Hi all.
    I am converting some of my PHP code to a snippet, and can’t get the current logged in user call to work.

    Even with just a new snippet:
    //----------------------------
    $username = $modx->getLoginUserName();
    echo $username;
    //----------------------------

    It doesn’t echo anything...
    Am I doing something wrong.

    I have tried to look at other snippets on how to retrieve the logged in user - but they don’t seem so straight forward.
    Cheers,
    FuzzeeMic
    • The getLoginUserName returns a web user’s name if you are in the "frontend", and a manager user’s name if you are in the "backend". So if you are in the "frontend" and trying to get a manager user’s username, this won’t work. You’ll have to get it directly from the SESSION.

      if(isset($_SESSION['mgrShortname'])) { $username = $_SESSION['mgrShortname']; }
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 10329
        • 2 Posts
        Wow - that was quick! grin

        You’re a legend - that did the trick.

        A big thankyou.
        FuzzeeMic
        • Insomnia. Figure I might as well put the time to good use. wink
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 6173
            • 34 Posts
            I had the same problem and its solved with the $username = $_SESSION[’mgrShortname’]; solution you posted.

            Now i just want to ask why the $modx->getLoginUserID function does’nt work in this situation ?
            • Because the function checks for if you are in the front-end, and if so, it will return the web user’s name. It will only return a manager user’s name if you are in the manager. This is determined by the index.php that is used, the one in the root sets the "in-front-end" constant, while the one in the Manager sets the "in-manager" constant. I suppose that the presumption was that you would only want web users if you were in the front-end, and manager users if you were in the Manager. It would be better if the function took an argument, "web" or "mgr", and returned the relevant value. Actually, that would not be difficult to do.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 32963
                • 1,732 Posts
                Right on target susan.

                cwbsoft you might want to log this as a feature request. In the future this problem will be eliminated with the arrival of 1.0 and beyond.
                  xWisdom
                  www.xwisdomhtml.com
                  The fear of the Lord is the beginning of wisdom:
                  MODx Co-Founder - Create and do more with less.
                  • 6173
                  • 34 Posts
                  I look forward to the arrival of 1.0 ... but for now im quite happe as long as i know to work around the "feature"  grin

                  By the way .. thanks for the quick reply
                    • 34162
                    • 1 Posts
                    //----------------------------
                    $username = $modx->getLoginUserName();
                    echo $username;
                    //----------------------------

                    It doesn’t echo anything...
                    But how is it possible that it doesn’t echo at all??
                    It should either return a webuser’s or a manager’s name, but not nothing!
                    function getLoginUserName(){
                        if($this->isFrontend() && isset($_SESSION['webValidated'])) {
                          return $_SESSION['webShortname'];
                        }
                        else if($this->isBackend() && isset($_SESSION['mgrValidated'])) {
                          return $_SESSION['mgrShortname'];
                        }
                      }

                    Apparently we have detected a situation where the program thinks that neither isFrontend nor isBackend resolves to true or expected Session values are not set.
                    I found this behaviour in other cirumstances too, that’s why I post in this thread.
                    There is so many "hidden logic" in MODx, that returning nothing is not acceptable.
                    It should at least return an error-code. So each and every function must return something, like Java code, which ususally ends with a throw exception directive. In this case: another elseif branch, who makes sure that something not as meaningless as "nothing" will be returned.
                      • 32963
                      • 1,732 Posts
                      Hi Paul,
                      
                      The logic of getLoginName() basically returns either the current web user or manager user name or a nothing if the user is not logged in. That's by design. In your case I would assume that the user is logged which means that something "could" be wrong with your with your modx installation.
                      
                      Please try the following:
                      
                      $tmp = $modx->isFrontEnd() ? 'front ':'';
                      $tmp.= $modx->isBackend() ? 'back ' : '';
                      <br />$tmp.= isset($_SESSION[’webValidated’]) ? ’Web login ’ : ’’;<br />$tmp.= isset($_SESSION[’mrgValidated’]) ? ’ manager login’ : ’’;
                      return $tmp;

                      If the user is logged in and sessions are working ok then one of the above should be displayed.

                        xWisdom
                        www.xwisdomhtml.com
                        The fear of the Lord is the beginning of wisdom:
                        MODx Co-Founder - Create and do more with less.