We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39148
    • 4 Posts
    Hi,

    Coding isn't my strong point as you can probably guess from this question!

    I am revamping a site and have decided to use MODX to make future maintenance easier. The problem I have been stuck on all day is regarding the capture of a variable from the url.

    The user lands on our page with or without an affiliate tracking paramater appended to the url: -

    eg. www.mydomain.co.uk/?int=123

    This variable then needs to be stored in a session so that if the user moves around the site the tracking code stays with them and if required added to a link that sends them to our quote engine: -

    eg http://quote.mydomain.co.uk/index.php?int=123

    I have this working on our existing site by simply using the following script: -

    <?
    session_start();

    // store session data
    $_SESSION['int']=$int;

    if ($int == '')
    {
    $int='0000';
    }

    ?>

    And then to redirect the user: -

    header('Location: http://quote.mydomain.co.uk/index.php?int='.$int);

    For some reason I can't get it to store the variable within modx. I have read that you don't need to use session_start in modx and have tried other avenues but keep going round in circles!

    I would appreciate any help!!

    Thanks
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The session is already started as soon as MODx gets the page request. Either a plugin or a snippet at the top of your template would do the trick. Fetch the value from the $_GET, validate it, and load it into the $_SESSION. Keep in mind that all $_GET and $_POST values are strings, not numbers. Something like this:
      $int = is_numeric($_GET['int']) ? $_GET['int'] : '0000';
      // other validation or sanitizing of the incoming GET value here
      $_SESSION['int'] = $int;
      

      Then your link would be
      'quote.mydomain.co.uk/index.php?int='.$_SESSION['int']
      [ed. note: sottwell last edited this post 14 years, 6 months ago.]
        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
        • 39148
        • 4 Posts
        Thanks Susan, I appreciate the help.

        I'm still having problems passing a value through. I'm developing/testing locally at the moment....would that have any impact?

        I have a snippet GetIntCode at the top of my template and call it by [[GetIntCode]]

        This snippet looks like this as advised: -

        <?php
        $int = is_numeric($_GET['int']) ? $_GET['int'] : '0000';
        
        $_SESSION['int'] = $int;
        
        ?>
        


        Then I simply use another snippet quoteonline to redirect: -

        <?php
        
        header('Location: http://quote.mydomain.co.uk/index.php?int='.$_SESSION['int']);
        
        ?>
        


        When I test by going to localhost/mydomain/index.html?int=100 the int variable holds 0000.

        At the risk of sounding dumb any ideas?

        Many thanks again.

          • 39148
          • 4 Posts
          That last redirect url should be: -

          header('Location: http://quote.mydomain.co.uk/index.php?int='.$_SESSION['int']);

          Think the forum editor added some stuff!