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
-
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
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.
-
☆ 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.
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).
That’s odd. I wonder what you were doing differently from me. I just set a $_SESSION var and it becomes available globally.
You should be able to do that with either $_SESSION or $GLOBALS, actually.