We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51020
    • 670 Posts
    Quote from: BobRay at Oct 17, 2016, 06:43 PM
    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 '';

    I just tried that - but it said the syntax on this line was wrong:

    $key = $modx--->context->get('key');
    


    I changed it to:

    $key = $modx->context->get('key');
    


    Was that just a typo?

    I tried it with the amended line above, and it still redirect to the prior page, rather than the one I want to land on.

    Do you know if my Login snippet is correct though?

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


    I have commented out the loginResourceId as I assume that would conflict with what we're trying to achieve?

      • 3749
      • 24,544 Posts
      That wasn't a typo, the extra characters were added by the forum software.

      Your Login tag looks right to me. I'm afraid I can't think of any more things to try.

      When using sendRedirect(), Login should see the private page as the "prior" page. I don't know why it isn't.
        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
        Hmm - anyone else have any bright ideas?

        So my privatePage snippet, which is included in the private page template is this:

        <?php
        /* PrivatePage snippet */
        /* Redirect anyone who is not logged in */
           
        $key = $modx->context->get('key');
        if (! $modx->user->hasSessionContext($key)) {
            $loginId = '81'; /* 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 '';
        


        and my login snippet which is on the login page is this:

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


          • 51020
          • 670 Posts
          So I tried this on a separate fresh Modx install in case there was something on my site which was stopping it working, but it still isn't working - it still redirects to the previous resource, rather than the resource you were trying to access before logging in.

          Any ideas anyone?