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

    when a user is logged in via WebLoginPE, I’d like to show him a page where data from a second table is gathered by a sql query based on his username. I thought I could use the user.username or view.username placeholders to put them into the query string of the snippet (WHERE username = [+user.username+] ..) but then realized, that this doesn’t work.

    It seems I even can’t retrieve the username from the modx_web_users table via API as there it is combined with the password into a hash.

    So do I have to store all related data I want to show in the modx_web_user_attributes_extended table?

    Or what would be the best way/variable to identify a logged in user for a individual query - maybe saved zo a cookie?

    May be it’s too simple for me at this moment to see the solution (as mostly in MODx), any help is appreciated -

    Thanks!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You can get a good deal of user information from the SESSION. http://www.sottwell.com/article-sessions.html

      There are also MODx API functions, http://wiki.modxcms.com/index.php/API:getLoginUserID and http://wiki.modxcms.com/index.php/API:getLoginUserName if you don’t want to directly access the SESSION yourself.

        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
        • 12110
        • 122 Posts
        Thanks Susan, this should help!

        It’s a joy to work with you and the MODx community.

        P.S.: This did the trick to get the username:
        echo $_SESSION['webShortname'];


          • 28042 ☆ A M B ☆
          • 24,524 Posts
          A snippet really should "return" the desired value. While "echo" works, it can be unpredictable in where the echo output will be. Since snippet code is passed through the eval() function, it should be treated as a function and always return, even if it’s just an empty return; at the end.

          Using the $modx->getLoginUserName() API function has the advantage of first making sure that the user is in fact a logged-in web user.
            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
            • 12110
            • 122 Posts
            Thanks, great!