Hi All,
It’s my first post here so please be gentle.
I am an experienced PHP dev and have been enjoying MODx now for a couple of months or so but I have one question where the answer seems to be evading me.
Can any of you please tell me if there is a standard MODx API way to pass $_POST and $_SESSION variables from one MODx page to another via snippets?
I want to test this by banging a simple chunk based form on one page and posting it to another page and using a snippet to drag out the data I posted and display it on that page. Along with some current data out of the session array as well.
Any help would be greatly appreciated as I’m hoping this is just a mental block I am having at the moment.
Cheers
Paul
-
☆ A M B ☆
- 427 Posts
Any php script can access the $_SESSION
There are a couple of ways:
if ($modx) {
you are already in the MODx environment and have access to its $_SESSION;
put and get at will anything in or from the $_SESSION
}
If the user is not logged in there is no $_SESSION available to attach to.
I would suggest you looking at a few of the snippets in the Repo to familiarize yourself on how it has been done before.
In essence: you do it like you would outside of MODx, just using snippets.
You should have $_SESSION data available even if no user is logged in.
-
☆ A M B ☆
- 427 Posts
Correct:
An Actual MODx Revolution Sample of a anonymous user:
[code line numbers added]
01 Array
02 (
03 [modx.user..attributes] => Array
04 (
05 [web] => Array
06 (
07 [modAccessContext] => Array
08 (
09 )
10
11 [modAccessResourceGroup] => Array
12 (
13 )
14
15 [modAccessElement] => Array
16 (
17 )
18
19 )
20
21 )
22
23 [webDocgroups] => Array
24 (
25 )
26
27 [modx.user.contextTokens] => Array
28 (
29 )
30
31 )
You have to include the MODX Session function.
require_once ’manager/includes/config.inc.php’;
Jason Buck
-
☆ A M B ☆
- 24,524 Posts
If this is from one MODx page to another, the SESSION is already started and running. Just access the $_SESSION with a snippet. Posting a form from one page to the other will make the $_POST available in the second page just like any form does. Again, use a snippet to access the $_POST. Of course, SESSIONs can time out, so cookies are also useful when maintaining data from one page to another (such as a user’s language choice in multilanguage sites).
In such a case, you would set a SESSION value and a COOKIE based on the document ID of the main menu item the user chose (this would be the language menu, and these documents would comprise the top-level row of documents in the Tree), and that snippet would be followed by a FirstChildRedirect snippet to send the visitor to his preferred language homepage, which is the first child of the top-level language document. A snippet in the template would get this value from the SESSION and/or the COOKIE. That snippet’s output would be used for the &startId of the Wayfinder snippet for that page, thus generating the main site menu for the correct language section, no matter what "extra" pages the visitor may have been on (such as a generic Contact Form page, perhaps).