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

    I’m trying to use $_SESSION variables to store values across pages using Revo contexts. The code I have is working fine across pages on the default ’web’ context, but when I try and access the session variables when running on a different domain in a different context, the stored values are not available to me.

    I can see in the modx_session table of my installation database that my session variables are being written ok to the database, but I can’t retrieve them when in anything but the default context.

    Am I missing something? Perhaps I need to add a context-specific session setting? Or maybe I need to initialise the session classes when I switch contexts?

    Any help much appreciated,
    Dave
      • 22303 MODX Staff
      • 10,725 Posts
      You must login to each context explicitly in order to share session information across them. In addition, you will have to be on the same domain or have your session cookies set to work for any subdomain of a specific domain if you are using Contexts across subdomains. You cannot share session information across Contexts if you are using them to represent separate domains because they will have different sessions based on the session cookies.
        • 11243
        • 27 Posts
        Thanks OpenGeek for shedding some light on this, but I don’t think I explained the problem very well first time round as I don’t want to share session information ’across’ contexts, I just want to be able to share it across pages which are in the same context as each other (that is, in a context other than the default web context).

        What I am trying to do is store the page id in a $_SESSION[’visited’] array of every page visited on the site by a casual visitor (who is not logged in) so that I can create a visual historical trail of previously visited pages. So basically, all session info is stored on the same domain/context. I really don’t want to have to force visitors to log in to provide this functionality. It already works this way when the site is built under the default web context! Now that I’ve moved the site to a different context/domain new session info cannot be retrieved.

        Is it possible to do something like: [tt]$visited = $modx->resource->getObject(’modSession’,array(’context’ => ’mycontext’, ’session_name’ => ’visited’);[/tt] ?

        Hope this makes more sense now?
        Dave

          • 22303 MODX Staff
          • 10,725 Posts
          Not really understanding the problem. If you access the same domain / session cookie it should work fine... huh
            • 11243
            • 27 Posts
            Quote from: OpenGeek at Apr 14, 2011, 09:01 AM

            If you access the same domain / session cookie it should work fine... huh

            That’s what I’d have expected too! But I just can’t get $_SESSION variables working within contexts other than the default context?

            Here’s a simple test I set up to prove the point...

            Create a snippet ’sess_init’:
            <?php
            $_SESSION['pageid'] = $modx->resource->get('id');
            ?>


            Create a second snippet ’sess_print’:
            <?php
            $_SESSION['pagelist'][] = $_SESSION['pageid'];
            print_r($_SESSION['pagelist']);
            ?>


            Call these 2 snippets in the page template eg.
            <body>
            [[!sess_init]]
            [[!sess_print]]
            </body>


            When the page is displayed in the browser, first time through it should print something like:
            Array ( [0] => 1 )
            (where 1 is the current page ID)

            Then every time you refresh, it will increment the array with the same page id:
            Array ( [0] => 1)
            Array ( [0] => 1 [1] => 1)
            Array ( [0] => 1 [1] => 1 [2] => 1 )
            

            This works fine within my default web context, but if I try it within any other context, the array doesn’t increment and every time I refresh the page I get the same:
            Array ( [0] => 5)
            (where 5 is the ID of the page in my other context)

            As the contents of $_SESSION[’pagelist] can’t be retrieved it is reinitialising the array each time instead of incrementing it.

            Hope this explains the problem better? FYI:
            MODx version 2.0.8-pl
            PHP version 5.2.17
            mysql version 5.0.91-log

            Cheers, Dave
              • 22303 MODX Staff
              • 10,725 Posts
              How are you routing requests to your contexts? Are you using the a plugin to switch contexts? Are they separate domains? Are they subdomains? This is all about the session cookies...
                • 11243
                • 27 Posts
                Sorry, yes, I should have included this info earlier...I actually modified my index.php file to switch contexts and yes, they are separate domains.

                Here’s the code (inserted around line 90 of index.php):
                <?php
                /* setup for multiple domains */
                switch(strtolower(MODX_HTTP_HOST)) {
                   case 'domain1.co.uk:80':
                   case 'domain1.co.uk':
                      // if the http_host is domain1.co.uk, switch the context to ctxt1
                      $modx->initialize('ctxt1');
                      $modx->switchContext('ctxt1');
                      break;
                   case 'domain2.co.uk:80':
                   case 'domain2.co.uk':
                      // if the http_host is domain2.co.uk, switch the context to ctxt2
                      $modx->initialize('ctxt2');
                      $modx->switchContext('ctxt2');
                      break;
                  default:
                      // by default / if no other rule can be applied, load 'web' context
                      $modx->initialize('web');
                      $modx->switchContext('web');
                      break;
                }​


                I did originally try the plugin method with little success and so it is disabled in my installation.

                Any ideas?
                  • 22303 MODX Staff
                  • 10,725 Posts
                  First, you don’t need to switchContext() if you initialize() that context in the index.php. You are loading the entire Context twice for no reason.

                  But WRT the original problem, all three domains will have separate session cookies. In addition, www. and the domain itself without www. will also have separate session cookies unless you set a session_cookie_domain value of .domain.tld in your configuration and then flush all sessions.
                    • 11243
                    • 27 Posts
                    Quote from: OpenGeek at Apr 16, 2011, 02:47 PM

                    First, you don’t need to switchContext() if you initialize() that context in the index.php. You are loading the entire Context twice for no reason.

                    But WRT the original problem, all three domains will have separate session cookies. In addition, www. and the domain itself without www. will also have separate session cookies unless you set a session_cookie_domain value of .domain.tld in your configuration and then flush all sessions.

                    This is getting worse...I did as suggested, removed the redundant switchContext() statements and added a session_cookie_domain key with a value of mymgrdomain.co.uk to my configuration setting and flushed all sessions, but now I can’t log in to my manager at all (as any user)!!! huh It doesn’t give any login errors, just leaves me on the login page with re-directing to the manager control panel! I’m sure that this is tied into the same session not being retained problem but the other sites are still working as before! Any ideas?!
                      • 6228
                      • 249 Posts
                      You should go into your browser and delete all cookies for your domain, if you haven’t already.
                        lo9on.com

                        MODx Evolution/Revolution | Remote Desktop Training | Development