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

    I need a global variable for each visitor,
    that can be used in scripts around the site.

    php $_SESSION array is perfect for that.

    but I’m loosing session info on page changing in ModX (while the code works great in outer phps)

    1. setting variable snippet:
    <?php
    if(isset($_POST['month'])){
    session_start();
    $_SESSION['day'] = $_POST['day'];
    $_SESSION['month'] = $_POST['month'];
    $_SESSION['year'] = $_POST['year'];
    }
    


    2. getting variable snippet
    <?php
    if(isset($_SESSION['month'])){
    session_start();
    $day=$_SESSION['day'];
    $month=$_SESSION['month'];
    $year=$_SESSION['year'];
    }
    else {
    $day = date(d);
    $month= date(m);
    $year= date(Y);
    }
    
    echo "$day-$month-$year";
    


    Both snippets are on every page.
    I’m loosing all session info on page change.
    If I refresh page - info stays.

    caching is turned off, modx revo 2.0.4

    are there any built-in opportunities to store that variable?
      • 19121
      • 11 Posts
      sorry, silly syntax mistake.

      should check code again before posting.

      everything working now grin
        • 22303 MODX Staff
        • 10,725 Posts
        Do not use session_start() in MODx components. MODx handles starting and closing the session.
          • 19121
          • 11 Posts
          some errors again (

          1. If I dont use session_start() in snippet - data vanish on every page change.
          2. If i use it - it works, but not for long. In some moment it sticks to some date,
          and dont change it in $_SESSION. cache lag?

          ---
          I’m almost started gnawing my guiltiness keyboard, and them mentioned session_handler_class in ModX settings.
          Turned it off. It works! So happy =) grin