We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39583
    • 103 Posts
    I have an external script that works like a gateway. When a user enters here a session is created and the user is redirected to a page within modx.

    session_start();
    $_SESSION['validSession'] = 'yes';


    Thin in modx when entering any page I have a snippet running that should check is session variable. But when I look for that session variable its empty.

    This is how I look for it in the snippet:

    if ($_SESSION['validSession'] != 'yes') {
        ...
    }


    How can I find this session variable in modx?

    This question has been answered by sottwell. See the first response.

    [ed. note: phacer last edited this post 13 years, 10 months ago.]
      --
      André
    • discuss.answer
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You have to set the session_name to be the same as the MODx session. Around line 19 of the core/config/config.inc.php file defines the session name; this is a unique value generated during installation.

      So you can just include the core/config/config.inc.php file in your .php script, and use the $session_name.

      include 'path-to-/core/config/config.inc.php';
      session_name($session_name);
      session_start();


      You may also be able to use a snippet for the script, as the content of a resource with no template. In that case it would already have all the MODx functionality. It depends on how the script is being implemented.
        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
        • 22303 MODX Staff
        • 10,725 Posts
        If you want your script to authenticate a MODX user and redirect them to a protected page, you will need to log the user in to the appropriate Contexts in MODX from the same domain as the site. Do not create the session yourself if you expect MODX to use it, because MODX has very specific requirements for populating the session data. If your goal is to start a session and use MODX within your script, then you should be able to manage your session as you desire to.
          • 39583
          • 103 Posts
          Thanks Susan,
          your second suggestion with a new resource worked very well. =)
            --
            André
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            I use this for AJAX processing all the time. In fact, such a resource-with-snippet can have several functions for dealing with however many AJAX requests your site may make; you don't have to have a separate resource for each AJAX-based function.

            Makes life much easier for a lazy programmer!
              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
              • 39184
              • 24 Posts
              Susan or phacer, would either of you care to elaborate on that method? I have a modal window for an school alert that will fire on any page load since I'm not sure where the user will enter from but it will need to be seen (school related). However, I don't want it to appear again as the user browses the site. I'm looking for the most efficient way to check for a session.
                • 39583
                • 103 Posts
                Hello BReynolds,

                I use a snipped that checks for a session value when each page loads. So put the call in a chunk or template that you use in the page where you want to display an alert.

                if ($_SESSION['yourSessionName'] != "sessionExist") {
                    //code for alert.
                
                    $_SESSION['yourSessionName'] = "sessionExist";
                }
                


                What this does is to check if a session with that variable name and value exists. If not an alert could be shown and the variable and value is set so you don't display the alert next time.
                  --
                  André
                  • 39184
                  • 24 Posts
                  Thanks...I'm going to give this a shot. It looks like what I need but I was getting confused on the whole deal.
                    • 39184
                    • 24 Posts
                    @phacer - pardon my ignorance on this subject. I haven't worked in coding PHP in a while and I'm still learning MODx. I plugged in your suggested snippet and I it registers the session as expected. My question is how do I translate that in to my modal window firing or not? I am using the "If" plugin to do some conditional stuff on whether the alert modal is turned on by a user setting. If you could somehow tie you plugin into that eval I'd be all set. It is just beyond me on how to pass that type of info in MODx.
                      • 39583
                      • 103 Posts
                      Quote from: breynolds13 at Feb 13, 2013, 10:06 AM
                      @phacer - pardon my ignorance on this subject. I haven't worked in coding PHP in a while and I'm still learning MODx. I plugged in your suggested snippet and I it registers the session as expected. My question is how do I translate that in to my modal window firing or not? I am using the "If" plugin to do some conditional stuff on whether the alert modal is turned on by a user setting. If you could somehow tie you plugin into that eval I'd be all set. It is just beyond me on how to pass that type of info in MODx.

                      I'm pretty new to MODx myself, but I think the following should work. I haven't tried it myself but I've tried similar things which works. =)

                      Snippet:
                      <?php
                      if ($_SESSION['yourSessionName'] != "sessionExist") {
                          $_SESSION['yourSessionName'] = "sessionExist";
                          return "0";
                      } else {
                          return "1";
                      }
                      


                      in your if statement:
                      [[!If?
                         &subject=`[[snippet]]`
                         &operator=`EQ`
                         &operand=`0`
                         &then=`display alert`
                         &else=`do nothing`
                      ]]
                      
                        --
                        André