We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 2912
    • 315 Posts
    Hi All,

    Is there a way to create a setting when accessing MODx that I can check for in another PHP page to prevent direct accessing without going through MODx pages?

    I hope thats a good enough description. Bit of a pain!! smiley

    Thanks all,

    BBloke
      BBloke
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Both the front index.php file and the manager index.php file already do this:
      define("IN_ETOMITE_PARSER", "true"); // provides compatibility with etomite 0.6 and maybe later versions
      define("IN_PARSER_MODE", "true");
      define("IN_MANAGER_MODE", "false");
      

      and
      define("IN_ETOMITE_SYSTEM", "true"); // for backward compatibility with 0.6
      define("IN_MANAGER_MODE", "true");  // we use this to make sure files are accessed the manager instead of seperately.
      

      Then in your php file you just check to see if the appropriate values are set "true".
        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
        • 2912
        • 315 Posts
        Thanks for your reply. It has helped tremendously but sadly it hasn’t worked as planned.
        I get what you are saying and have tried the following constructs to allow this to work:

        if (isset($IN_PARSER_MODE)) {
        ...
        if ($IN_PARSER_MODE != true) {
        ...
        if (!defined ("IN_PARSER_MODE")) {	
        	exit();
        }
        

        I have also tried adding a little text found on php.net (included the define statement in the index.php page) but to no avail.

        if (!defined("X")) {
           echo "You Cannot Access This Script Directly, Have a Nice Day.";
           exit();
        }
        

        It all looks good but it doesn’t allow access even when I sign in or have gone through index.php of modx site sad

        Am I still doing something wrong?
          BBloke
          • 33372
          • 1,611 Posts
          Check out this page on PHP constants:
          http://www.php.net/constants

          You refer to them like IN_PARSER_MODE (not $IN_PARSER_MODE or "IN_PARSER_MODE").
            "Things are not what they appear to be; nor are they otherwise." - Buddha

            "Well, gee, Buddha - that wasn't very helpful..." - ZAP

            Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
            • 2912
            • 315 Posts
            Thanks ZAP,

            I have cleaned up the text and done some more testing and its still a no go. I’ve got the PHP page set to echo IN_PARSER_MODE and instead of returning the setting it comes back as IN_PARSER_MODE.

            My code is:
            if (!defined(IN_PARSER_MODE)) {	
            	echo IN_PARSER_MODE;
            	exit();
            }
            

            I dont understand what wall I’m hitting but then it is Monday. sad
              BBloke
              • 33372
              • 1,611 Posts
              Maybe try this:

              define("IN_PARSER_MODE", "true");
              if ( !defined(IN_PARSER_MODE) ) echo 'IN_PARSER_MODE not defined';
              else echo IN_PARSER_MODE;
              exit();
                "Things are not what they appear to be; nor are they otherwise." - Buddha

                "Well, gee, Buddha - that wasn't very helpful..." - ZAP

                Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                • 2912
                • 315 Posts
                Thanks for your quick reponse ZAP. Its all helpful.

                I added the code to the page I’m restricting.

                If I leave:
                define("IN_PARSER_MODE", "true");

                in then it comes back true.

                If I take it out so it looks like:
                if ( !defined(IN_PARSER_MODE) ) echo 'IN_PARSER_MODE not defined';
                else echo IN_PARSER_MODE;
                exit();
                


                I get not defined.

                This happens in both cases, with or without modx.

                I’m getting more lost as time goes on sad

                Edit: I’ve done some more testing and I can see that this should work but I fear there is something very odd with my setup. I’ve done some further testing and if I add the following snippet:
                <?php
                define ("X", "TRUE");
                echo X;
                echo IN_PARSER_MODE;
                ?>

                and call that via a chunk:
                [[codecheck]]
                <div>
                <iframe name="iframe1" src="/index.php" align="top" height="600" width="100%" hspace="0" vspace="0" border="0"></iframe>
                </div>
                <br /><br />Then my echo’s come back with the following:<br />

                TRUEtrue
                IN_PARSER_MODE not defined
                <br /><br />The first TRUE relates to X, the second true relates to IN_PARSER_MODE, the third callback: IN_PARSER_MODE not defined relates to the code inside /index.php<br /><br />I’m going to work on using snippets to create a constant and see if I can include /index.php call from inside the snippet that way.<br /><br />Thanks to you both for your help. It has come in exceptionally handy. :)<br /><br />BBloke
                  BBloke