We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36875
    • 5 Posts
    A client of mine has made a Wordpress site on the same domain where I built his Modx site. Let's say the Wordpress site is in www.thewebsite.com/wp. He has asked me if it's possible to restrict access to www.thewebsite.com/wp to web users who are logged in to Modx.

    It seemed like a simple question at first but the more I think about it, the more I think it can't be done. How can you possibly figure out if a user is logged in or not, when you're in a folder that has no relation to Modx at all (except that it happens to be on the same domain)? Is there a way or should I just tell him to forget about it?
      • 3749
      • 24,544 Posts
      Maybe you could write a session variable with a plugin connected to OnWebLogin and check that variable in WP?


      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      MODx info for everyone: http://bobsguides.com/MODx.html
        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
        • 36875
        • 5 Posts
        Hi BobRay,

        Thank you very much for your suggestion. It seems like a good idea but I'm a bit stuck again.

        I wrote a plugin that sets a session variable. No problem there. Then, after reading https://forums.modx.com/thread/13645/accessing-modx-session-vars-from-external-scripts I put this bit of code in the WP index file in order to be able to read the session variable:

        require_once '../core/model/modx/modx.class.php'; 
         
        $modx = new modX();
        $modx->initialize('web');
         
        if ($_SESSION['mysessionvar'] === false) {
          //proceed
        


        Now WP keeps saying 'Fatal error: Class 'modX' not found in [long path]'. However, no errors are reported on the require_once. So if it has modx.class.php, why can't it find the class?
          • 36875
          • 5 Posts
          It's working now. Think I may have made a typo earlier. I'll summarize what I did for anyone else who's trying to do something similar.

          In order to make Wordpress check if a user is logged in to Modx, I wrote a plugin that looks like this:
          $eventName = $modx->event->name;
          switch($eventName) {
              case 'OnWebLogin':
                  $_SESSION['wordpressforbidden'] = false;
                  break;
              case 'OnWebLogout':
                  $_SESSION['wordpressforbidden'] = true;
                  break;
          


          and connected it to the OnWebLogin and OnWebLogout events.

          and in the file index.php in Wordpress' root folder I put this:
          require_once '../config.core.php'; 
          require_once MODX_CORE_PATH.'model/modx/modx.class.php'; 
           
          $modx = new modX();
          $modx->initialize('web');
          if ($_SESSION['wordpressforbidden'] === false) {
          
          // all the stuff that was in index.php already
          
          }
          else {
           echo 'not authorized';
          }
          exit();
          


          Of course, this solution is not perfect, because the modification in Wordpress may be overwritten if Wordpress gets to be updated.
            • 3749
            • 24,544 Posts
            Looks good. Thanks for reporting back. One suggestion: I would use a key in the $_SESSION variable rather than false, just to make it a little more secure.



            ---------------------------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
            MODx info for everyone: http://bobsguides.com/modx.html
              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