We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51020
    • 670 Posts
    Hi there.
    I am using the privatePage snippet (thanks Bob!) which is great for a simple members area I have on the site. One problem though, is that after a successful login, all users are redirected to one specific resource, instead of the resource they were trying to access when the snippet intercepted.
    is there a way to expand the snippet so as to push the user to the destination they were trying to access in the first place?

    Thanks!
    Andy
      • 3749
      • 24,544 Posts
      Do you have &redirectToPrior=`1` in the Login snippet tag?
        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
        • 51020
        • 670 Posts
        Quote from: BobRay at Oct 12, 2016, 04:40 PM
        Do you have &redirectToPrior=`1` in the Login snippet tag?

        Thanks Bob - No but I did try that - but takes the user back to the page PREVIOUS to the one I'm trying to access if that makes sense.

        So, the user is on the home page, then click 'MEMBER PAGE' - and the privatePage snippet intercepts and takes them to the Login page (but wihtout the URL changing, so it still thinks it's on the member page) - so after logging in, it takes them back to the PRIOR page, which is the home page.

        Does that make sense?

        I think the redirectToPrior is actually doing as it should, but the browsers still thinks it's on the member page.

        I'm running V2.5.1 and Login is all up to date.

        Thanks
        Andy
          • 3749
          • 24,544 Posts
          That does make sense.

          Everything below assumes that you are using &redirectToPrior=`1`.

          In the snippet, are you using sendRedirect() or sendForward()? If it's sendForward(), try sendRedirect().

          If it's sendRedirect, you might try some of the possible second arguments to see if they make a difference: https://docs.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.sendredirect

          The form would be:

          $modx->sendRedirect($url, array('REDIRECT_HEADER');


          If none of that works, you could try getting the URL of the current page and setting the $_SERVER variables. I'm not sure it would work:

          That would look like this (above the redirect):

          $docId = $modx->resource->get('id');
          $currentUrl = $modx->makeUrl($docId, "", "", "full");
          
          $_SERVER['REQUEST_URI'] = $currentUrl;
          $_SERVER['HTTP_REFERER'] = $currentUrl;
          



            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
            • 51020
            • 670 Posts
            Quote from: BobRay at Oct 14, 2016, 01:42 AM
            That does make sense.

            Everything below assumes that you are using &redirectToPrior=`1`.

            In the snippet, are you using sendRedirect() or sendForward()? If it's sendForward(), try sendRedirect().

            If it's sendRedirect, you might try some of the possible second arguments to see if they make a difference: https://docs.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.sendredirect

            The form would be:

            $modx->sendRedirect($url, array('REDIRECT_HEADER');


            If none of that works, you could try getting the URL of the current page and setting the $_SERVER variables. I'm not sure it would work:

            That would look like this (above the redirect):

            $docId = $modx->resource->get('id');
            $currentUrl = $modx->makeUrl($docId, "", "", "full");
            
            $_SERVER['REQUEST_URI'] = $currentUrl;
            $_SERVER['HTTP_REFERER'] = $currentUrl;
            




            Thanks for this Bob.
            Which snippet are you referring to?
            I have looked in the privatePage and Login Snippet, and cannot find anything that refers to sendForward or sendRedirect.
            Not sure where else to look for it.

              • 3749
              • 24,544 Posts
              I meant the PrivatePage snippet. Oddly, I don't actually have a copy of it, so I can't check, but it must redirect the user to the Login page and I don't know how else that could happen.
                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
                • 51020
                • 670 Posts
                Quote from: BobRay at Oct 15, 2016, 10:22 PM
                I meant the PrivatePage snippet. Oddly, I don't actually have a copy of it, so I can't check, but it must redirect the user to the Login page and I don't know how else that could happen.
                Ah ok sorry. See below:

                <?php
                /* PrivatePage snippet */
                /* Redirect anyone who is not logged in */
                 
                $key = $modx->context->get('key');
                if (! $modx->user->hasSessionContext($key)) {
                   $modx->sendUnauthorizedPage();
                }
                return '';
                
                  • 3749
                  • 24,544 Posts
                  Try this:

                  <?php
                  /* PrivatePage snippet */
                  /* Redirect anyone who is not logged in */
                    
                  $key = $modx->context->get('key');
                  if (! $modx->user->hasSessionContext($key)) {
                      $loginId = '12'; /* change this to the ID of your Login page !!! */
                      $loginUrl = $modx->makeUrl($loginId, "", "", "full");
                      $modx->sendRedirect($loginUrl);
                  }
                  return '';


                  Be sure to clear the cache before testing.

                  If it doesn't work, try adding this just above the sendRedirect() line:

                  $docId = $modx->resource->get('id');
                  $currentUrl = $modx->makeUrl($docId, "", "", "full");
                   
                  $_SERVER['REQUEST_URI'] = $currentUrl;
                  $_SERVER['HTTP_REFERER'] = $currentUrl;
                    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
                    • 51020
                    • 670 Posts
                    Quote from: BobRay at Oct 16, 2016, 03:30 PM
                    Try this:

                    <!--?php
                    /* PrivatePage snippet */
                    /* Redirect anyone who is not logged in */
                      
                    $key = $modx--->context->get('key');
                    if (! $modx->user->hasSessionContext($key)) {
                        $loginId = '12'; /* change this to the ID of your Login page !!! */
                        $loginUrl = $modx->makeUrl($loginId, "", "", "full");
                        $modx->sendRedirect($loginUrl);
                    }
                    return '';


                    Be sure to clear the cache before testing.

                    If it doesn't work, try adding this just above the sendRedirect() line:

                    $docId = $modx->resource->get('id');
                    $currentUrl = $modx->makeUrl($docId, "", "", "full");
                     
                    $_SERVER['REQUEST_URI'] = $currentUrl;
                    $_SERVER['HTTP_REFERER'] = $currentUrl;


                    Thanks Bob - still not working - it redirects to the PRIOR, but that's not what I need - I need it to redirect to the page I was a bout to access before it was intercepted by the login page.
                    I added the extra part of the snippet, but if I come form the home page, and try to access the member page, afterwards, it directs me back to the home page.

                    ON this login page I have this:

                    
                    [[!Login?
                    &loginTpl=`lgnLoginTpl`
                    &logoutTpl=`lgnLogoutTpl`
                    &errTpl=`lgnErrTpl`
                    &logoutResourceId=`111`
                    &redirectToPrior=`1`
                    ]]
                    
                    


                    I have taken out the code:
                    &loginResourceId=`3`
                    
                    


                    In case that was overriding what you sent, but still not working - I think the problem is that its trying to redirect to a page that it hasn't reached yet?

                      • 3749
                      • 24,544 Posts
                      Did you it this way?

                      <?php
                      /* PrivatePage snippet */
                      /* Redirect anyone who is not logged in */
                         
                      $key = $modx--->context->get('key');
                      if (! $modx->user->hasSessionContext($key)) {
                          $loginId = '12'; /* change this to the ID of your Login page !!! */
                          $loginUrl = $modx->makeUrl($loginId, "", "", "full");
                          $docId = $modx->resource->get('id');
                          $currentUrl = $modx->makeUrl($docId, "", "", "full");
                        
                          $_SERVER['REQUEST_URI'] = $currentUrl;
                          $_SERVER['HTTP_REFERER'] = $currentUrl;
                          $modx->sendRedirect($loginUrl);
                      }
                      return '';
                        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