We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8394
    • 24 Posts
    modx 1.0.5
    WAMP
    Windows XP SP 3
    Apache 2.2.17
    MySQL 5.1.54
    PHP 5.3.5
    Firefox 3.6.15 / Opera 11.10

    I’ve tried Googling "modx sessions" etc., but every page I’ve found seems to be jumping in at the middle of the topic; I can’t find any documentation that talks about how modx deals with sessions starting at the beginning.

    My basic problem is this:

    I have two snippets (in the same "chunk" that defines common features for all web pages for my site), as follows:

    HTML stuff
    .
    .
    [snippet1]
    .
    HTML stuff
    .
    .
    [snippet2]
    .
    .
    etc.

    In snippet1 I set a session variable, like:
    $_SESSION[’qqq’] = ’zzz’;
    If I test it there, like:
    echo isset($_SESSION[’qqq’] . ’ ’ . $_SESSION[’qqq’] . ’</br>’;
    it works as expected and I get something like:
    yes zzz
    displaying on my web page.

    However, if I try to reference that session variable in snippet2, like:
    echo isset($_SESSION[’qqq’] . ’</br>’;
    I get a ’no’, and I simply cannot do anything with that variable.

    Note that everything is controlled entirely within modx here, so there are no "external" web pages involved or anything like that. I initially (not realizing that modx does its own session handling) put in a snippet0 before anything else with:
    session_start();
    but then I removed that so as to rely on modx’s session handling. Alas, this made no apparent difference that I could see.... everything worked (or rather didn’t work) exactly the same.

    In desperation, I rebuilt some of the modx web pages OUTSIDE of modx entirely, replacing snippets with inline PHP code, etc., so that the final page was exactly what originally got generated by modx, and here the session variables worked exactly as I had expected, so I’m certain that modx is doing something itself with the session stuff that I am not allowing for.

    Can anyone point me documentation that gives an overview of the way modx deals with sessions (for beginners) and/or clear up what I’m doing wrongly in the above example?

    Many thanks,

    - Dave
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      It should work; as long as the session value is set in one snippet before the other snippet tries to access it.

      Keep in mind that uncached snippets aren’t processed until the post-processing, so if your setter snippet is uncached and the getter snippet is cached it won’t find the value. A cached snippet will only be run the first time the page is requested. Uncached snippets will always be run, as the resource is put through post-processing after it’s taken from cache. I prefer to use a plugin to load a SESSION value, and set it as a placeholder.

      There isn’t much documentation simply because MODx doesn’t do anything at all unusual with the SESSION.

      http://www.sottwell.com/article-sessions.html
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 8394
        • 24 Posts
        Susan, thanks very much for your quick reply. I had already found your page on sessions (that you provided a link for), and had pondered it at some length, but didn’t get any closer to understanding my particular situation.

        The snippets I included are (for now) uncached (in the form [!snippet1!]) just to remove one more variable from the whole equation, but in this case that really shouldn’t make a difference since I see the problem on the first call to the web page after restarting my browser. And both snippets are, indeed, being executed because I have put in "echo HTML display stuff;" in both of them just to make sure I see clearly what is happening.

        When I tried this outside of modx I copied all the chunks that comprise my starting web page and pasted them into a flat .php file, and then went through and replaced each snippet call with the corresponding php code from modx. The resulting code was as identical as possible to what would is generated by modx (verified by looking at the page source in both cases via my browser), and yet the behavior is very different in the two cases. From modx, the session variable is unset in the second snippet, while running the .php flat file directly in my browser I find that the session variable IS set in the second php block.

        Sorry, but I’m still stumped!

        Thanks again for getting back to me, and I’ll keep pounding on it at this end. If I manage to figure out what is different I’ll post it here in case anyone else encounters the same situation, but in the meantime if you or anyone else can think of anything more I should try I will be glad to do so.

        - Dave

          • 8394
          • 24 Posts
          After lots of head scratching and hair pulling I finally decided to make changes on my workstation, but display those changes on my wife’s computer just to save time. But I found to my surprise that it all worked perfectly on her computer. But still not on mine!

          Hmmmm....

          So then I decide to log out of modx on my computer and, lo and behold... it works there now!

          Apparently being logged into modx is doing something with sessions that overrides what I am trying to do. I guess this makes a lot of sense since modx has to maintain a session in order to keep me logged in. Duh! laugh Great to know, but definitely awkward. I need to be logged in in order to work on the site, but if I have to log out every time I want to test something that will certainly add a prohibitive amount of time to development.

          So given this latest wrinkle, anyone have any suggestions?

          Thanks as always!

          - Dave
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            The only time this is a problem is if you’re accessing the Manager and the front-end as different subdomains, that is if one is www.domain.com while the other is domain.com; they’ll each get their own SESSION. I have my .htaccess file rewrite all attempts to access www.sottwell.com to sottwell.com partly for SEO consistency and mostly to avoid these odd SESSION issues. Otherwise, I don’t see how there can be any difference unless you’re duplicating one of the Manager login’s session variable names.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 8394
              • 24 Posts
              I’ll have to admit that I’m quite new to web programming, so I don’t yet understand the nuances of sessions and their interaction with domain names, etc. Having said that, what you’re describing is how, on reflection, it seemed to me that it should be working. After all, we have sessions defined for (say) amazon.com and pandora.com, but those two sessions don’t interfere with each other.

              As far as setting a domain name, right now I am running locally on WAMP so my "domain" is simply localhost. Once I move everything up to our web hosting service it will have its own domain name, but I will have no control over that whatsoever... nor (presumably) will I have the ability to change things like the .htaccess file, although I may well be wrong about that. I haven’t actually tried that level of access for our web host, since I haven’t had enough yet to even test run up there.

              As for different domain names, I just went and checked... my starting web page is referenced in the browser simply as http://localhost, while the manager is referenced as http://localhost/manager, so the domain name seems to be the same in both cases.

              The session variable I am creating is called "cookies_allowed"... I guess it’s possible that modx manager is using an identically named session variable. Do you know if there is a list anywhere of the session variables that it uses?

              Thanks again for your help.

              - Dave
                • 8394
                • 24 Posts
                I figured out how to list the session variables used by modx with a little snippet:

                <?php
                foreach($_SESSION as $varname => $varvalue) {
                echo $varname . ": " , $varvalue . "
                ";
                }
                ?>

                Looking through the list of 20 or so variables there doesn’t appear to be a conflict with my cookies_allowed variable. So the domain name is the same and I’m simply setting a non-conflicting session variable... yet when I try to access that variable after escaping to HTML and then going back into PHP it is gone. But it isn’t gone if modx manager isn’t running. Hmmmm.... I’m clearly still missing something.

                - Dave
                  • 15083
                  • 697 Posts
                  have u another snippet running that may be killing your session?
                    • 8394
                    • 24 Posts
                    Quote from: jusmeig at Apr 29, 2011, 10:32 AM

                    have u another snippet running that may be killing your session?

                    Nope, pretty sure not. I have been testing it all morning and had just come here to post my findings when I saw your note, so I thought I’d reply to that first.

                    But here’s what I have found:

                    Using the little php snippet I mentioned earlier

                    <?php
                    foreach($_SESSION as $varname => $varvalue) {
                    echo $varname . ": " , $varvalue . "
                    ";
                    }
                    ?>


                    I can easily demonstrate that, with modx manager not running, the code

                    <?php
                    $_SESSION[’abc’] = ’Hello’;
                    ?>
                    HTML stuff
                    <?php
                    echo $_SESSION[’abc’] . ’
                    ’;
                    ?>


                    Does what it should: displays "Hello". However, with modx manager running (and nothing else changing) it produces a blank. The session variable ’abc’ still appears in the list of all session variables, but it is empty (or blank, I’m still not sure how to tell the difference in PHP) instead of "Hello".

                    So my question is: is this a bug or a feature? So far it’s caused me quite a headache, and I can’t fathom why it’s doing what it seems to be doing.

                    I greatly appreciate any other input into this.

                    Best regards,

                    - Dave