We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30448
    • 4 Posts
    Hello.
    I’m using ModX as the CMS for a website and part fo the requirements is the we need to be able to use sessions so people can log in and out (essential function), the trouble is that for some reason sessions within ModX don’t work. We put the code, say, for signin in into a snippet, which includes something to the effect of:
    [tt]session_start();
    $_SESSION[’myid’] = "bubbaJoeBob_Edna";[/tt]
    Now all the code works swimmingly... until we port it to ModX. After that it seems as though the sessions don’t work any more, as though whatever data we put into the session get erased automatically or something. We don’t know, we’ve been trying to figure it out for weeks with no luck. Everything works fine and dandy when we upload it to the site without loading it into ModX (ie, we just leave it in it’s plain jane form). We’ve chacked the code and the main template does include a session_start(); at the top, but despite that, the session doesn’t seem to be starting.

    That’s the problem. I’m wondering if there’s some sort of procedure we need to go through to activate sessions for ModX or what. Help is more appreciated.

    -Torin Mai
      • 25663 MODX Staff
      • 12,272 Posts
      MODx definitely has it’s own session handling routines and that’s likely what’s conflicting, so you can stop pulling your hair out. I suspect before the weekend is over you’ll have an answer from someone that can help you figure it out. There’s also the default weblogin and permissions system built into MODx that might be able to help you accomplish what you’re trying to do.
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 33372
        • 1,611 Posts
        You can still use session variables normally in MODx, though. I think you should try just commenting out the session_start(); and see if that fixes things. I store data in session variables like this without any problems.
          "Things are not what they appear to be; nor are they otherwise." - Buddha

          "Well, gee, Buddha - that wasn't very helpful..." - ZAP

          Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          right. Since MODx automatically starts a session on every page load, you don’t need to in your scripts. Just use the one MODx sets.

          Make a snippet with the code
          echo "<pre>";
          print_r($_SESSION);
          echo "</pre>";
          return '';

          and put the snippet in a document. You’ll get a good view of what MODx has in its session.

          I used the session in developing a simple shopping application (ShopX), and was even able to manipulate the sessions to copy the user data over to an https secure session when going to the secure checkout page.
            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
            • 30448
            • 4 Posts
            less than a day and already useful responses. Wow, you guys are great.
            I’ll have to test it on some of the more session heavy stuff but I think that the whole declaring session_start thing was causing the problem...somehow undecided
              • 33372
              • 1,611 Posts
              Basically MODx already starts and maintains a session for each user, so by starting another one either MODx or your snippet code will have trouble finding the right information (and MODx will win in this case).
                "Things are not what they appear to be; nor are they otherwise." - Buddha

                "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                • 33372
                • 1,611 Posts
                That’s odd. I wonder what you were doing differently from me. I just set a $_SESSION var and it becomes available globally.
                  "Things are not what they appear to be; nor are they otherwise." - Buddha

                  "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                  Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                  • 33372
                  • 1,611 Posts
                  You should be able to do that with either $_SESSION or $GLOBALS, actually.
                    "Things are not what they appear to be; nor are they otherwise." - Buddha

                    "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                    Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options