We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24245
    • 9 Posts
    Hello,
    i have already tried serveral things inside a snippet to get this to work, but it doesn’t.
    $modx->getLoginUserID(); or
    $modx->getLoginUserName();
    both return NULL
    although I am logged in inside the manager. I must apologize for my stupid question but maybe anyone has a hint as to why this could be the case?

    thank you very much in advance
      • 16278
      • 928 Posts
      Looking at the code in manager/includes/document.parser.class.inc.php, it looks as if you’ll only see logged-in web users in the front end using these functions. I found that was the case with a simple test page and snippet - nothing returned for manager login, ID or name returned when I logged in as a web user.

          # Returns current user id
          function getLoginUserID($context= '') {
              if ($context && isset ($_SESSION[$context . 'Validated'])) {
                  return $_SESSION[$context . 'InternalKey'];
              }
              elseif ($this->isFrontend() && isset ($_SESSION['webValidated'])) {
                  return $_SESSION['webInternalKey'];
              }
              elseif ($this->isBackend() && isset ($_SESSION['mgrValidated'])) {
                  return $_SESSION['mgrInternalKey'];
              }
          }
      
          # Returns current user name
          function getLoginUserName() {
              if ($this->isFrontend() && isset ($_SESSION['webValidated'])) {
                  return $_SESSION['webShortname'];
              }
              elseif ($this->isBackend() && isset ($_SESSION['mgrValidated'])) {
                  return $_SESSION['mgrShortname'];
              }
          }
      
        • 4041
        • 788 Posts
        You can get alot of logged in user info from the SESSION to use in your snippets. Note that the admin (back end) and web users (front end) are seperate, so you have to create a web user account for yourself (if you haven’t already).

        Make you a new snippet named Session_Info with the following code, call it in a document ( [!Session_Info!] ), log in to either the manager(admin) or as a web user (or both), navigate to that page and it will show you whats available.

        <?php
        /* Session_Info
           [!Session_Info!]
        */
        $output = print_r($_SESSION);
        
        return $output;
        ?>


        edit: A quick example of using existing session variables in a snippet:
        <?php
        /*    WebUser_Redirect
        
        */
        $output ="";
        if(isset($_SESSION['webValidated']) && isset($_SESSION['webShortname'])){
          $web_UserLoginHomeID =$_SESSION[webUsrConfigSet][login_home];
          if($web_UserLoginHomeID !=""){
        
                 // grab the current page id and compare to the users home id
                 // if it matches then print something, if not it creates the link or placeholder
                  $currentdoc = $modx->documentIdentifier;
                  if($currentdoc == $web_UserLoginHomeID){
                   // in this example it prints: Breezer's Home Page (Breezer would be the logged users name)
                    $output .=$_SESSION['webShortname']."'s Home Page";
                  }else{
                   // output a simple text link wherever this snippet is placed
                    $output .="<a href='[~".$web_UserLoginHomeID."~]'>Home Page</a>";
        
                   // or set a placeholder to use in the script later
                    $output .= $modx->setPlaceholder("WULoginHomeID", "$web_UserLoginHomeID");   // [+WULoginHomeID+]
        
                   // example of placeholder usage
                    $output .="<a href='[~[+WULoginHomeID+]~]'>Home Page</a>";
                  }
           }
        }
        
        return $output;
        ?>
          xforum
          http://frsbuilders.net (under construction) forum for evolution
          • 24245
          • 9 Posts
          Thank you so much - your replies helped me understand a lot more (yes, i’m not at all firm in php - yet)

          now it works smiley

          i put
          $output .= ' '.$_SESSION['mgrFullname'];

          in my snippet, I tried something similar yesterday... strange thing, was it showed me the name, just after i logged in with managerlogin (want to use quickedit), but as soon, as i clicked any other page, the name was gone again. But now it seems to work smiley

          • I needed to do the same thing yesterday and then found this: http://www.modxcms.com/UserPlaceholders-1596.html

            I just called the snippet on any pages where I expect a user to be logged in then use TV Place holders to output any details about the user.

            Aaron
              http://www.onesmarthost.co.uk
              UK MODX Hosting with love.