We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24041
    • 7 Posts
    I’ve run into a very odd problem to do with session control on a site I’m building.

    Briefly, the snippet I’ve written opens a session, and then sets a variable called ’terr’ depending on which of 5 different page’s id is returned by $modx->documentIdentifier

    This snippet appears on every page of the site but should only be set if one of the page ids matches the 5 selected ones.

    The snippet works fine on the pages where an id matches, but on pages were they don’t, the snippet seems to makes a match with page 138 which is my site start page.

    The snippet looks like this:
    
    session_start();
    
    $pageid = $modx->documentIdentifier;
    
    switch($pageid) {
    
        case '138' : $_SESSION['terr'] = 'eur'; break;
        case '115' : $_SESSION['terr'] = 'lon'; break;
        case '129' : $_SESSION['terr'] = 'par'; break;
        case '179' : $_SESSION['terr'] = 'loc'; break;
        case '180' : $_SESSION['terr'] = 'crew'; break;
    
        default : $_SESSION['terr'] = $_SESSION['terr']; break;
    }
    
    


    This could be something really stupid (I’m sure it is) but two of us have been at this for a couple of days now and we’re close to tears...
      • 22303 MODX Staff
      • 10,725 Posts
      First, you never need to call session_start() in a MODx snippet. MODx automatically starts a session itself.

      Second, are you sure this snippet is called so that it is never cached? i.e. [!Snippet!] vs. [[Snippet]] ?
        • 24041
        • 7 Posts
        Thanks for your reply, I’ve taken out the session_start bit and verified that this snippet is definitely not being cached, but the result is still the same.

        Incidentally, if I comment out the line in the switch related to ’138’, then everything works (aside from that particular page, of course). The effect is the same if I set the id match to a non-existent page, like ’999’. It’s almost as if page 138 is being matched on every other page that doesn’t appear in the switch statement, as if it were the default.
          • 22303 MODX Staff
          • 10,725 Posts
          I’m really not following the logic of the Snippet at all here, but I’ll have to believe you. wink
            • 24041
            • 7 Posts
            PHP and sessions etc. aside, I suppose the real question would be; is there any reason or any point in the document parsing process where documentIdentifier might confuse with the site_start id with the current page id?

            (guessing no, but I’m getting a bit desperate and this is the only thing I can think of...)
              • 24041
              • 7 Posts
              We just tried setting the site_start to a different page altogether and the result was the same, so it looks like this is just a weird co-incidence that the problem id is the site start.

              So forget all of the above, we’ve got a different problem somehow *sigh*

              Thanks for trying to help
                • 24041
                • 7 Posts
                For anyone who’s interested or perhaps having a similar problem, we cracked it:

                It turns out it was the association of page 138 with the site’s 404 error page, which was also set to 138, the same as the site start. So, not a coincidence after all. As soon as we set this 404 page to something else, the snippet and session control all began to work fine.

                Without wishing to draw any wild conclusions, it does appear that something is finding the site’s error page during the process of navigating from one page to another. The snippet was then apparently matching this id with one of the cases in the switch statement and setting the ’terr’ variable to ’eur’, as that line in my function dictates.

                This snippet is being called from the first line of a chunk that appears at the very top of each template.

                How this relates to sessions and documentIdentifier, I have no idea.