We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43084
    • 34 Posts
    Quote from: BobRay at Mar 26, 2013, 04:46 PM
    The Login page is a form that submits to itself, so when you click on the Login button, SaveReferer will execute again with (I think) the Login page as the referer.

    If you put the SaveReferer tag below the Login tag, it should only execute once, but it should also make sure not to set the $_SESSION variable if it's already set (in case the user enters the wrong info and the page reloads).

    Thanks, that tidbit about the Login page submitting to itself is what I was missing. I changed the code in my SaveReferer script to the following:

    if ($_SERVER['HTTP_REFERER'] != 'http://www.<mysite>.com/index.php?id=1'){
    $_SESSION['CallingPage'] = $_SERVER['HTTP_REFERER'];
    }


    And now I'm getting the correct value!

    I tried putting this code back in my PreHook snippet, but this same code returns a blank if run from there, meaning that $_SERVER['HTTP_REFERER'] will always return the Login page if called from there.

    So I just left my 'SaveReferer' snippet call as shown above (before the call to the 'Login' snippet even) in the Login page and it worked great. Then, to redirect to the correct page after Login I created the following PostHook:

    if ($_SESSION['CallingPage'] != ''){
    $modx->sendRedirect($_SESSION['CallingPage']);
    }
    return true;


    And now everything works like a charm!

    I suspect that now I'm completely overriding the &redirectToPrior=`1` parameter, but I'll leave it there anyways for now. You know, if it's not broken... smiley

    Thanks BobRay for all the help. Your suggestions and pointers in the right direction made all the difference!
    • discuss.answer
      • 3749
      • 24,544 Posts
      I'm glad it's working for you. Just a thought -- your code will throw a PHP warning it that server variable is not set, so that makes it vulnerable to mucking up the output if the host changes the error reporting level. I would suggest something like this instead:



      if ( (isset($_SERVER['HTTP_REFERER'])) && (! empty($_SERVER['HTTP_REFERER'])) &&  ($_SERVER['HTTP_REFERER'] != 'http://www.<mysite>.com/index.php?id=1'))  {
             $_SESSION['CallingPage'] = $_SERVER['HTTP_REFERER'];
      }


        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
        • 43084
        • 34 Posts
        Thanks a lot! I just updated and tested the code and everything looks fine. Once again, thank you very much!
          • 3749
          • 24,544 Posts
          My pleasure. smiley

            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