We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29517
    • 40 Posts
    I have a site with 3 logins. The usernames are BOARD, ZONE and COMMITTEE. The login form is on page 163.

    When a BOARD member logs in, they need to be redirected to page 164.
    ZONE members are taken to page 165
    COMMITTEE members to 166.

    How do I achieve this? I've read splittingred's comments in another post suggesting to use a posthook so this is what I've tried with no luck.

    Login snippet call:
    [[!Login? 
    &loginTpl=`lgnLoginTpl` 
    &logoutTpl=`lgnLogoutTpl` 
    &errTpl=`lgnErrTpl` 
    &postHooks=`redirect` 
    &logoutResourceId=`163`]]
    


    For each of the three users, in Manage Users>Extended Fields I've added the attribute "redirect - 164" (or 165,166)

    I then created a Snippet named "redirect":
    <?php
    $profile = $modx->user->getOne('Profile');
    if ($profile) {
      $extended = $profile->get('extended');
      if (!empty($extended['redirect'])) {
        $url = $modx->makeUrl($extended['redirect'],'','','full');
        $modx->sendRedirect($url);
      }
    }
    return true;
    


    Is this all I need to do to get the redirection happening, or am I missing something?

    This question has been answered by multiple community members. See the first response.

      Creative Director - Throwstone Web Design Melbourne
      • 3749
      • 24,544 Posts
      I think there might already be a snippet called 'redirect'. Try using a different name for yours.

      Also, you might try this to make sure you're getting the profile and redirect value:

      if ($profile) {
        $extended = $profile->get('extended');
        if (!empty($extended['redirect'])) {
          $url = $modx->makeUrl($extended['redirect'],'','','full');
          $modx->sendRedirect($url);
        } else {
            die('No redirect value');
        }
      } else {
          die('No Profile');
      }


      BTW, it appears that you could do without the extended field and just test the username:
      $username = $modx->user->get('username');
      
      $pageId = $modx->getOption('site_start');
      
      switch ($username) {
            case: 'BOARD':
                $pageId = 164;
                break;
            case: 'ZONE':
                $pageId = 165;
                break;
            case: 'COMMITTEE':
                $pageId = 166;
                break;
      }
      
      $modx->sendRedirect($modx->makeUrl($pageId, "", "", 'full'))
      ; [ed. note: BobRay last edited this post 12 years, 4 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
        • 29517
        • 40 Posts
        Thanks. I've changed the snippet name to 'landingPage' and replaced the snippet code with what you provided above so that it looks like this:

        <?php
        $profile = $modx->user->getOne('Profile');
        if ($profile) {
          $extended = $profile->get('extended');
          if (!empty($extended['redirect'])) {
            $url = $modx->makeUrl($extended['redirect'],'','','full');
            $modx->sendRedirect($url);
          }
        } else {
            die('No Profile');
        }
        


        It's not picking up the profile so I see 'No Profile' upon logging in. I've added the following below my snippet call to see what's going on.
        [[!Login? 
        &loginTpl=`lgnLoginTpl` 
        &logoutTpl=`lgnLogoutTpl` 
        &errTpl=`lgnErrTpl` 
        &postHooks=`landingPage`
        &logoutResourceId=`163`]]
        
        [[!Profile]]
        <p>[[+username]]'s landing page is [[+redirect]]</p>
        


        When I log in as BOARD it successfully displays BOARD's landing page is 164. But that's after I see the 'No Profile' message and refresh the page to clear it.

        The access permissions are working fine (ie. when logged in as BOARD, I can view the BOARD page and not the other two pages - I just can't land on the BOARD page upon logging in). Can I safely assume that my problem has nothing to do with Context Access, Roles, etc.?

        I've cleared Permissions, flushed Cache. Any other suggestions?








          Creative Director - Throwstone Web Design Melbourne
          • 29517
          • 40 Posts
          I'm heading down the path of testing the username instead of using extended fields and I've reduced the snippet code of 'landingPage' to this:
          <?php
          $username = $modx->user->get('username');
           
          $pageId = '163';
           
          switch ($username) {
            case 'BOARD':
                    $pageId = '164';
                    break;
            case 'ZONE':
                    $pageId = '165';
                    break;
            case 'COMMITTEE':
                    $pageId = '166';
                    break;
          }
          
          $landingPage = $modx->makeUrl($pageId);
          $modx->sendRedirect($landingPage);


          It's still not working, but if I call the snippet [[!landingPage]] independently from some other page, it recognizes the current username and redirects me correctly. Something is therefore going wrong when the snippet is called via
          [[!Login? ... &postHooks=`landingPage`]]
          It can't seem to detect the username or profile.
            Creative Director - Throwstone Web Design Melbourne
          • discuss.answer
            • 3749
            • 24,544 Posts
            Try putting this as line 3:

            die( ' Username:  ' . $username);


            You may have to use one of these:

            $username = $hook->getValue('username');
            $username = $hook->getValue('login.username');


            Also, always generate a full URL with makeUrl():

            $landingPage = $modx->makeUrl($pageId, "", "", 'full');
              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
              • 29517
              • 40 Posts
              It works! Using
              $username = $hook->getValue('username');

              returns the username, whereas
              $username = $hook->getValue('login.username');
              returns nothing and
              $username = $modx->user->get('username');
              returns 'anonymous'.

              What's bizarre is that the resulting snippet is virtually identical to Lucas' solution posted at http://tiny.cc/ex5tn which was the first thing I tried. Didn't work then, does now.

              For anybody else reading this, the working snippet is:
              <?php
              $username = $hook->getValue('username');
              
              $pageId = '163';
               
              switch ($username) {
                case 'BOARD':
                        $pageId = '164';
                        break;
                case 'ZONE':
                        $pageId = '165';
                        break;
                case 'COMMITTEE':
                        $pageId = '166';
                        break;
              }
              
              $landingPage = $modx->makeUrl($pageId, "", "", 'full');
              $modx->sendRedirect($landingPage);


              Thanks Bob for taking the time to help me out with this. Incidentally, the tutorials on Bob's Guides were the first resources I read when considering switching to MODX Evo some years ago. It helped me set up my first test site, and you're still helping me out now! Much appreciated!
                Creative Director - Throwstone Web Design Melbourne
                • 3749
                • 24,544 Posts
                I'm glad you got it working. Thanks for reporting back. smiley

                I don't know why $modx->user is the anonymous user. It's possible that that object just hasn't been updated yet at the point where a postHook runs.
                  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
                  • 29110
                  • 17 Posts
                  Just want to thank #terobot and #BobRay for this : saved my bacon after having struggled to work this out myself. After reading this I got individual private member pages working beautifully.

                  Much respect to you both.
                  • How would I do this if I want to redirect the user based on User Group? I have two User Groups with multiple users assign to each. I tried replacing username with userGroup, but it did not work.
                      Lee Brinckley
                      Manassas, VA (Washington DC)
                      Twitter - @leebrinckley
                      http://www.leebrinckley.com
                    • discuss.answer
                      Lee I think you would need to use the isMember from the API to check if the user is part of a specific group and route them accordingly.

                      http://rtfm.modx.com/display/revolution20/modUser.isMember
                        Benjamin Marte
                        Interactive Media Developer
                        Follow Me on Twitter | Visit my site | Learn MODX