We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32098
    • 31 Posts
    Steve asked a related question about sessions 45 days ago and didn’t provoke a response.

    My situation is this: I have a form in a sidebar which will print below itself a list of things the user is looking for. With one snippet and a bucketfull of chunks the form is working. Now, I want that list that is printed below to be carried over to subsequent pages so that the user doesn’t have to start the search again later. Bob’s Guide gave me the clue that sessions were the way to have values passed from one HTML page to another, but I can’t find anything on how to use sessions in Modx Revolution.

    I’ve been trying to get to grips with the PHP from Larry Ullman’s book "PHP6 and MYSQL5" but I imagine the techniques described there (like putting anglebracketopen ?php session_start(); ?anglebracketclose) are not going to work here.

    So how does a newbie go about setting up sessions and using them to store values between pages? Or is it a mistake to even try?
      • 22303 MODX Staff
      • 10,725 Posts
      MODx automatically starts and ends sessions with each request made to the site. You can simply save values into the $_SESSION array and they will be saved in between requests so you can use them on subsequent pages (so long as you have the same user session). Not really any magic to it other than don’t call the session functions yourself to start, end, or otherwise manipulate the session configuration—that can all be done via settings in MODx.
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Simply add your own stuff to the $_SESSION array just as you would to any other array. MODx starts its session automatically; you don’t have to worry about it.
        $_SESSION['mystuff'] = array('itemOne' => 'value1', 'itemTwo' => 'value2', ...);
          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
          • 32098
          • 31 Posts
          Thanks Jason and Susan for the amazingly prompt replies. You guys seem totally tireless and incredibly patient.

          Am now going to give it a go.
            • 32098
            • 31 Posts
            After quite a few hours of agony and self-doubt I seem to have managed to get the Sessions working so that a list of searched-for items can be displayed on successive pages that share the same snippet call - the same snippet that uses chunks to set the search form up and also processes the search query.

            I was hoping that it would have been the sort of learning experience that would put me in a position to write something to really help others who might be similarly clueless. But no. There was too much trial-and-error stuff - trying things out without having a clue whether they should work or not, and realising I really didn’t know whether sometimes I was dealing with strings or arrays or some strange hybrid.

            But if people are looking for a clear explanation of how to use sessions, the clearest I found over the last few hours is here: http://www.phpro.org/tutorials/Introduction-To-PHP-Sessions.html

            As pointed out by Jason and Susan, there is no need to put session_start() at the top of the relevant pages.

            So far the following seems to be working for me, but it breaks the rules by sneaking in some presentation stuff, and I am sure there are other problems. Here is an excerpt with a while loop used to work through a query result. It stores the results of the while loop in a session variable.

            $resultb = mysqli_query($dblink, $queryb);
              while ($row = mysqli_fetch_assoc($resultb)) {
                     
                          $var1 = $row['desc'];
                          $var2 = $row['alias'];
                           $_SESSION['searchoutput'][] = '<li><a href=" '. $var2  . '.html">' . $var1 . '</a></li>';
             
            } // end while loop
            


            Then to print out the array stored in the session variable my guess was that I needed something like this:

            if(isset($_SESSION['searchoutput'])) {
                 foreach($_SESSION['searchoutput'] as $key=>$value){
                   $toprint = $value;
                }
                  $output = $modx->getChunk('searchchunk', array(
            	       'searchresults' => $toprint,
            	));
            	return $output;
            }


            That unpacks the array and sends the contents to the chunk called ’searchchunk’, which contains the placeholder [[+searchresults]].

            Larry Ullman’s book on good old-fashioned procedural PHP made PHP seem easy. I’m going to stick with Modx, but I have to admit that it is heavy going. There seems to be a massive leap required to get from the cosy stuff described by Larry to the kind of thing required by Modx.

            Thanks again for the excellent support.
              • 44258
              • 33 Posts
              Quote from: webdesig at Jan 04, 2011, 11:45 AM
              After quite a few hours of agony and self-doubt I seem to have managed to get the Sessions working so that a list of searched-for items can be displayed on successive pages that share the same snippet call - the same snippet that uses chunks to set the search form up and also processes the search query.

              I was hoping that it would have been the sort of learning experience that would put me in a position to write something to really help others who might be similarly clueless. But no. There was too much trial-and-error stuff - trying things out without having a clue whether they should work or not, and realising I really didn't know whether sometimes I was dealing with strings or arrays or some strange hybrid.

              But if people are looking for a clear explanation of how to use sessions, the clearest I found over the last few hours is here: http://www.phpro.org/tutorials/Introduction-To-PHP-Sessions.html

              As pointed out by Jason and Susan, there is no need to put session_start() at the top of the relevant pages.

              So far the following seems to be working for me, but it breaks the rules by sneaking in some presentation stuff, and I am sure there are other problems. Here is an excerpt with a while loop used to work through a query result. It stores the results of the while loop in a session variable.

              $resultb = mysqli_query($dblink, $queryb);
                while ($row = mysqli_fetch_assoc($resultb)) {
                       
                            $var1 = $row['desc'];
                            $var2 = $row['alias'];
                             $_SESSION['searchoutput'][] = '<li><a href=" '. $var2  . '.html">' . $var1 . '</a></li>';
               
              } // end while loop
              


              Then to print out the array stored in the session variable my guess was that I needed something like this:

              if(isset($_SESSION['searchoutput'])) {
                   foreach($_SESSION['searchoutput'] as $key=>$value){
                     $toprint = $value;
                  }
                    $output = $modx->getChunk('searchchunk', array(
              	       'searchresults' => $toprint,
              	));
              	return $output;
              }


              That unpacks the array and sends the contents to the chunk called 'searchchunk', which contains the placeholder [[+searchresults]].

              Larry Ullman's book on good old-fashioned procedural PHP made PHP seem easy. I'm going to stick with Modx, but I have to admit that it is heavy going. There seems to be a massive leap required to get from the cosy stuff described by Larry to the kind of thing required by Modx.

              Thanks again for the excellent support.
              thanks for this. this really works.