We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3677
    • 130 Posts
    og, I am using the method outlined here in my gateway plugin:
    http://rtfm.modx.com/display/revolution20/Using+One+Gateway+Plugin+to+Manage+Multiple+Domains

    It is using modx->switchcontext and not modx->initialize().
    What are the implications for sessions?
      • 22303 MODX Staff
      • 10,725 Posts
      You have to make sure the initialized context is starting the session properly. Regardless, it will be for the requested domain unless you've hardcoded the session_cookie_domain in your System or Context Settings.
        • 3677
        • 130 Posts
        You have to make sure the initialized context is starting the session properly

        I'd like to do that but I don't understand the system well enough. I am monitoring cookies in multiple browsers and I can see that by the time the first page finishes loading, there is definitely a PHPSESSID cookie available to the browser and the value is unique from browser to browser.

        I've had some trouble setting a Session var and reading it back before the HTML head code is complete on the landing page though. I need to understand exactly when in the code the cookie gets dropped and it is possible to start storing Session Vars.

        -Brian
          • 3677
          • 130 Posts
          Ok, I've gotten a little further...

          I am able to set other session vars in my gateway plugin and retrieve them for display on the landing page. I am not able to retrieve the PHP session ID on the initial page rendering though...it shows up after a refresh of the page. Here is the code that runs in the gateway:

              if (isset($_COOKIE["PHPSESSID"]))
              $sID = $_COOKIE["PHPSESSID"];
              $_SESSION['sID'] = "$sID";


          So on the 1st page request, the cookie is not set according to this check "isset($COOKIE["PHPSESSID"]" and yet I am able to assign session variables since other vars I am setting and retrieving work ok.

          Then after a page refresh, the code returns true (cookie is set now) and the Session var contains the actual PHP session ID.

          For the initial page request, the gateway code fires first and the above code is running before the switchContext command executes and sends it to the right resources. I guess the cookie is not actually created at this point, but the session vars can still be queued up?


            • 22303 MODX Staff
            • 10,725 Posts
            First, you should be using the session_name() function; MODX can be configured not to use the default session_name defined in php.ini when creating the session, so you need to use it to find the session name.
            if (isset($_COOKIE[session_name()]) {
                // code here
            }


            Second, you can always get the current session_id with the function session_id().

            Third, setting the session_id into the session data is a potential security risk. Why do you want to do this? As explained before, setting another unique value for the user in a non-session related cookie is another option, as are many others. Ignoring that, why can you not just set the session_id() into the $_SESSION?

            if (!isset($_SESSION['sID']) || $_SESSION['sID'] !== session_id()) {
                $_SESSION['sID'] = session_id();
            }


            Just make sure if this is in a Snippet it is executed before any other code (Snippet or Plugin) that expects the value to be set, i.e. place that Snippet first in the source of your Template or Resource content. Similarly, if in a Plugin, make sure it occurs on an event that is invoked before the Snippet( s ) and/or Plugin( s ) that depend on it are executed.
              • 3677
              • 130 Posts
              1. Got it. Thx.
              2. Sure enough! That is working. I thought I had tried it before but your code works fine.
              3. I don't know enough to know why this is a security risk but I'll take your word for it. I've got it working as well with just a random generated number for my ID so maybe I will stick with that now. I don't really need to reference the session id for any reason, just need a unique #.

              Thanks for the help.

              -Brian