We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7974
    • 8 Posts
    I thought snippets were php code. I know php and I am running into code that does not work in the snippets. The problem plagueing me now is how to do a header redirect? Here is the code line that is giving the error:

    header("Location: http://www.google.com");


    and here is the error that I get:

    « MODx Parse Error »
    MODx encountered the following error while attempting to parse the requested resource: 
    « Redirection attempt failed - please ensure the document you're trying to redirect to exists. 
    Redirection URL: /blog.html?refurl=%2Findex.php%3Fq%3Dblog.html%26refurl%3D%252Findex.php%253Fq%253Dblog.html%2526refurl%253D%25252Findex.php%25253Fq%25253Dblog.html%252526refurl%25253D%2525252Findex.php%2525253Fq%2525253Dblog.html%25252526refurl%2525253D%252525252Findex.php%252525253F%25252526err%2525253D1%252526err%25253D2%2526err%253D3%26err%3D4
    
    » 
      
    Parser timing 
      MySQL:  0.0052 s s (2 Requests) 
      PHP:  0.0299 s s   
      Total:  0.0351 s s   
    


    it looks like it is trying to find that url in the modx site. Why doesn’t it just send the code to the php interpreter?

      • 10487 MODX Staff
      • 1,535 Posts
      Hi DragonWize,

      Because of the way that the MODx parser executes snippets, using header() will not work (as you found out)

      However, you can use the MODx API to get the same effect:
      $modx->sendRedirect('http://www.google.com');


      Hope that helps,
      Garry

        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 7974
        • 8 Posts
        Thanks garryn,

        So obviously snippets are not just raw php like the "manager" proudly anounces. I realize the documentation and API are a little sparse at the moment but is there any form of list of what you can and can’t use?

        DragonWize
          • 7974
          • 8 Posts
          Sorry garryn,

          I was a little premature on thinking that would do it. I am still getting the same error. And the part that really gets me is that I searched the forums and the modx snippets code and found serveral places where they were using the header command as normal. Quite puzzling. I’ve refreshed the site. and even tried to use some of the methods in the document parser class with no avial.

          Thanks for the help any other suggestions?
          DragonWize
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Well, actually they are "raw" php. The snippet’s code is evaluated within the context of the parser’s output buffer. If you really wanted to use the php redirect you could, but you would have to close the parser’s output buffer and start your own. Or something like that, I’m not real familiar with how PHP output buffering works.

            http://il.php.net/manual/en/ref.outcontrol.php
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 7974
              • 8 Posts
              Thanks Susan. That did it. Was good information that script was running through and output buffer. I assumed it was doing a simple replace with in the template. For anyone searching the forums for redirecting in a snippet here is what you can do, this also applies to if you want to scrap whatever the document parser has done and you may write your own code.

              ob_end_clean(); // this will end the output buffer and discard silently what ever was in it
              header('Location: http://www.yoursite.com'); // now you may use header as normal


              You don’t have to worry about starting the output buffer back up in this case becuase the header is redirecting.

              Once again thanks Susan,
              DragonWize