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

    I did a strange experiment with session variables and iframes.

    In one master page, I have this code (here simplified) :
    session_start(); // create the session
    if (!session_is_registered('sessionStartTime')) {
    	$_SESSION['sessionStartTime']=time();


    In the child iframed php content, I have this code:
    session_start(); // retrieve existing session

    Later, in both the master and the child documents, I have these lines:
    echo "<h3>---".$_SERVER['HTTP_COOKIE']."---</h3>";
    echo "<h4>---".$_SESSION['sessionStartTime']."---</h4>"
    ;
    The framed content is called by a form submission, so the session exists and can be retrieved with session_start();

    The returned HTTP_COOKIE variable is the same for both documents.
    On the contrary to HTTP_COOKIE, ’sessionStartTime’ session variable cannot be retrieved in the framed document.

    Both documents belongs to the same domain name.
    Any idea why I cannot access the session variable ?

    Thanks.
      • 22303 MODX Staff
      • 10,725 Posts
      Are you sure the domain is exactly the same (i.e. not www. vs. non-www)? Also, if these are MODx documents, do not call session_start(); MODx does this automatically.
        • 5340
        • 1,624 Posts
        I had a similar problem when combining modx with an iframe generated with codeigniter.

        The solution was to create an ajax request to a page that reads the session. Based on the response I would redirect to another page.



          • 15001
          • 697 Posts
          Are you sure the domain is exactly the same (i.e. not www. vs. non-www)?

          Yes, I’m sure. However, the path to the iframed ressource is set relatively. So, there is no "www" in the value of the <form>’s "action" parameter.
          As explained, the session is retrieved correctly (same session number in cookie). Only session variables are not accessible from within the iframe. Maybe is this due to the browser configuration.

          Also, if these are MODx documents, do not call session_start(); MODx does this automatically.
          In my case, the parent document is a MODx document and the iframed one is a PHP script.

          Bye.
            • 9130
            • 171 Posts
            If you want an external php script to share the modx session you need to call session_name and set the name to same one your modx installation uses before starting the session.

            Look in your /manager/included/config.inc.php file at the startCMSSession function, I’m sure you will be able to figure it out.