We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7966
    • 90 Posts
    I can’t get the session variables preserved when a second page is visited.

    I set the session variables when a visitor first visit the website. But when I visit another page, the session variables are empty.

    I tested this before I moved the site to a new context by using the following method http://rtfm.modx.com/display/revolution20/Creating+a+Subdomain+from+a+Folder+using+Virtual+Hosts

    both w/out a session_start() has been tried.

    Do we need to make any specific changes for sessions to work when we need to handle multiple domains with contexts?

    Thank you
      • 7966
      • 90 Posts
      I learnt that session are recorded in modx_session table.

      checking this table shows that the first time a visitor comes, variables are set correctly. below are 2 records from this table.

      [table]
      [tr]
      [td]b7c1dcf743c65477f50184f7c2b6ff63[/td][td] 1288610628[/td]
      [td]searchengine|s:14:"www.google.com";keyword|s:23:"modx cms system ";modx.user.contextTokens|a:0:{}[/td]
      [/tr]
      [tr]
      [td]04f7e782d96ed8862547a1276db4d35a [/td]
      [td]1288610638 [/td]
      [td]searchengine|s:5:"other";keyword|s:9:"not found";modx.user.contextTokens|a:0:{}[/td]
      [/tr]

      [/table]


      First record, when a visitor comes, add the session variable to the table, second record, when visitor browses another page, is added as a result of checking if session variables exist. if not found, the code creates a new one. I use the following code to check for session variables.


      if( !isset($_SESSION['searchengine']) &&  !isset($_SESSION['keyword']) ){
         $_SESSION['searchengine']= $searchvalues['searchengine'];
         $_SESSION['keyword']=$searchvalues['keyword'];
      }



      for some reason, it is not reading the session variable although it is recorded.
        • 22303 MODX Staff
        • 10,725 Posts
        Do these contexts have different subdomains? If they do, did you set session_cookie_domain to the value:
        .domain.tld

        Note the opening . as this is the key to making sessions work across sub-domains. You cannot do this across different domains.
          • 7966
          • 90 Posts

          No I haven’t set anything.

          Context will be different domains, not sub-domains. Domains are on a shared-hosting, added as add-on domains.


          So, isn’t it possible to set session if we have different domains that we want to manage through a central modx manager?

          Thank you
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: dragona at Nov 01, 2010, 05:49 PM


            No I haven’t set anything.

            Context will be different domains, not sub-domains. Domains are on a shared-hosting, added as add-on domains.


            So, isn’t it possible to set session if we have different domains that we want to manage through a central modx manager?

            Thank you
            You manager should only be accessible on one domain when using a multi-context configuration. And no, you cannot share PHP sessions across different domains, only for a specific domain, or by prefixing a domain with a ., across all sub-domains within that domain.
              • 7966
              • 90 Posts
              Thanks for the explanation OpenGeek.

              I have no problem with manager being accessible on one domain, what I want is to add session variables for each domain, not to share any session data between these different domains.

              This is very restrictive then, if you want to use multi domains with one central manager. if I understand you right, this is not something possible technically for now or future releases, right?
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: dragona at Nov 03, 2010, 06:30 AM

                Thanks for the explanation OpenGeek.

                I have no problem with manager being accessible on one domain, what I want is to add session variables for each domain, not to share any session data between these different domains.

                This is very restrictive then, if you want to use multi domains with one central manager. if I understand you right, this is not something possible technically for now or future releases, right?
                I’m not following how this is restrictive at all. It’s simply how PHP sessions work in a browser. The session cookie is either set for a specific domain or any subdomain of a specific domain. The manager session would be separate from the domain specific sessions for each web site, so I don’t see how any session data would be shared between these different domains. When they login to the single manager URL on the domain you configure/designate for the manager, they get a session cookie for the manager’s domain.
                  • 7966
                  • 90 Posts
                  Quote from: OpenGeek at Nov 03, 2010, 11:31 AM

                  I’m not following how this is restrictive at all. It’s simply how PHP sessions work in a browser. The session cookie is either set for a specific domain or any subdomain of a specific domain. The manager session would be separate from the domain specific sessions for each web site, so I don’t see how any session data would be shared between these different domains. When they login to the single manager URL on the domain you configure/designate for the manager, they get a session cookie for the manager’s domain.

                  maybe I am little confused.

                  I understand that php sets a cookie for a specific domain, and obviously manager login domain will have its own session.

                  Actually scenario is as follow.

                  I have two domains, domain X and Y, using their own sessions. I want to move those 2 domains to modx using the contexts.

                  I have third domain, domain Z, which host the modx, its sole purpose is to host the modx.

                  two domain visitors only view the front-end pages - they don’t in any way use the modx manager domain nor login to manager interface as they work separately at the moment.

                  Sorry if you have already answered but my question is that is it possible for domain X and Y to have their own sessions. They don’t share any session either with each other or with modx manager domain Z.




                    • 34193
                    • 330 Posts
                    Quote from: OpenGeek at Nov 03, 2010, 11:31 AM

                    The session cookie is either set for a specific domain or any subdomain of a specific domain.

                    So each domain will have it’s own session and only it’s own session.
                      • 7966
                      • 90 Posts
                      Quote from: peteedley at Nov 04, 2010, 03:43 AM

                      Quote from: OpenGeek at Nov 03, 2010, 11:31 AM

                      The session cookie is either set for a specific domain or any subdomain of a specific domain.

                      So each domain will have it’s own session and only it’s own session.

                      Thanks peteedley. If that is the case, it is not working as it should in my case. I just wanted to be sure if each domain can have its own session state, since the code is very simple, it just writes the session on first page and reads on the second page.