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

    I know this has been answered a whole heap of times, but I can’t find anything regarding Revo.
    I need to set a couple of session variables in an external script which Revo then needs to be able to read.
    I used to do it in Evo using this:

    require_once ’manager/includes/config.inc.php’;
    startCMSSession();

    Anyone have any idea what I should be using in Revo?
    Sorry if I’m being dense and could easily work this out on my own, but I’m on deadline and being a typical panicky dev!

    Cheers,

    PeteWJ
      • 26903
      • 1,336 Posts
      For revo session start is now done when you initialise a modx object, like this $modx->initialize(’web’); see the initialize function, this calls _initSession. Doing it this way will give you a session per object initialised however, not access to an already running one for say a logged in user. You can try this, initialise three modx objects in an external script and you’ll see three sessions added to the sessions table.
        Use MODx, or the cat gets it!
        • 14660
        • 11 Posts
        Quote from: shamblett at Sep 03, 2010, 08:37 AM

        For revo session start is now done when you initialise a modx object, like this $modx->initialize(’web’); see the initialize function, this calls _initSession. Doing it this way will give you a session per object initialised however, not access to an already running one for say a logged in user. You can try this, initialise three modx objects in an external script and you’ll see three sessions added to the sessions table.

        I don’t understand what you mentioned there huh

        So exactly how would we be able to get the $_SESSION? Just add
        $modx->initialize('web');
        in the external file?
          • 26903
          • 1,336 Posts
          Yes, I’m just saying the start session call is now inside the creation of the modx object, it wasn’t in Evo.
            Use MODx, or the cat gets it!
            • 28189
            • 47 Posts
            Hi,

            I just wanted to post something that may be helpful to someone in the future...

            If you have previously started a session (session_start()) then the MODX session handler doesn’t take over.

            This Works:
            // Assuming $use_modx_sessions is a setting that's been set previously
            if ($use_modx_sessions) {
                require_once '/var/www/example.com/config.core.php';
                require_once MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php';
                require_once MODX_CORE_PATH.'model/modx/modx.class.php';
                $modx = new modX();
                $modx->initialize('web');
            } else {
                if (!session_id()) {
                    session_start();
                }
            }


            This Does Not Work:
            if (!session_id()) {
                session_start();
            }
            // Assuming $use_modx_sessions is a setting that's been set previously
            if ($use_modx_sessions) {
                // Note: session_destroy() doesn't help either
                require_once '/var/www/example.com/config.core.php';
                require_once MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php';
                require_once MODX_CORE_PATH.'model/modx/modx.class.php';
                $modx = new modX();
                $modx->initialize('web');
            }
            
              • 10344
              • 1 Posts
              Doing it this way will give you a session per object initialised however, not access to an already running one for say a logged in user.

              Hi!
              My question is how DO I get access to an already running session from an external php script? I succeeded in doing this for Evo, but I have no idea how to implement same functionality for Revo. embarrassed

                • 17284
                • 54 Posts
                I had this same issue with modx revolution, in case it’s not clear from this thread, which it wasn’t, this is how you start sessions from outside scripts:

                http://www.philiptrew.com/webdevelopment/modx/session-variables.html

                // Include files to declare the MODx core class
                require_once '/path_to_your/config.core.php';  
                require_once MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php';  
                require_once MODX_CORE_PATH.'model/modx/modx.class.php';  
                
                // Create an instance of the MODx core
                $modx = new modX();
                
                // Initialise the core to access the default 'web' context
                $modx->initialize('web');
                
                // Set a value in the desired session variable
                $_SESSION['my_session_variable']='my_value';
                


                You can set the first value that is gotten from: require_once ’/path_to_your/config.core.php’;
                which just declares two modx globals, like this:
                define('MODX_CORE_PATH', '/full/path/to/modxcore/' );
                define('MODX_CONFIG_KEY', 'config');


                which would give you:
                define('MODX_CORE_PATH', '/full/path/to/modxcore/' );
                define('MODX_CONFIG_KEY', 'config');
                require_once ( MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php' );  
                require_once ( MODX_CORE_PATH.'model/modx/modx.class.php' );  
                
                // Create an instance of the MODx core
                $modx = new modX();
                
                // Initialise the core to access the default 'web' context
                $modx->initialize('web');


                I’m running a very complex external application that needs a lot of session support, and this fix was the solution, now all the little glitches are gone. Simply create an include and add this to the top of each php file that needs it. Works, and solves the session issue, which as far as I know, is not possible to resolve without this method.

                Important: you must accept the default modx session name, do not try to mess with that. That’s probably going to be PHPSESSID, but verify this by checking your session cookies for the site, do not assume it. Also remember that modx revolution requires cookies, so SID appending to url isn’t going to work for you, you have to do the cookie checking explicitly if you require that.

                Thanks to the site that solved this issue, but since this thread came up in google search for the modx revolution start external script sessions, I thought I’d add the real solution to it
                  • 22303 MODX Staff
                  • 10,725 Posts
                  FYI, you should never directly include the MODX_CONFIG_KEY.inc.php file. Just leave that part off as it is included automatically when you call new modX().

                  <?php
                  // Include files to declare the MODx core class
                  require_once '/path_to_your/config.core.php';
                  require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
                    • 29201
                    • 239 Posts
                    Pete,

                    Could you share with me how to access MODx externally in Evo?

                    Thanks,

                    Eliot
                      • 36416
                      • 589 Posts
                      Quote from: taiyo1578 at Oct 09, 2011, 03:06 PM

                      Could you share with me how to access MODx externally in Evo?

                      http://wiki.modxcms.com/index.php/Use_AJAX_with_modxAPI