Passing sessions variables through an iframe, php

First timer to the site, not an overly experienced php programmer here :)

I have a problem, i am using an iframe within a site which im attempting to use a session variable inside, firstly ive just been trying to display the session variable to make sure they are accessible from within the iframe:

    echo "session of productcheck ".$_SESSION['productcheck']." 
"; echo "session of productcheck1 ".$_SESSION['productcheck1']."
"; echo "session of productcheck2 ".$_SESSION['productcheck2']."
"; echo "session of productcheck3 ".$_SESSION['productcheck3']."
";

this just shows "session of product check" with nothing after each one, I set the session variable as such:

$_SESSION['productcheck'] = $productBox;

the $productBox is a GET from the URL:

echo " <iframe src="homeview.php?productBox=$product1" name="FRAMENAME" width="594" height="450" scrolling="No" id="FRAMENAME" allowautotransparency="true" > </iframe >"; 

What's strange is if i just take the $productBox variable retrieved from the URL and use that then the code works, its only when i store it in a session variable that it gets confused. I want to retrieve a second $productBox and assign it to session var productcheck1 and so forth down the line. Unfortunately, i have to take one var in at a time, otherwise i could just pass all 4 products and not worry about the sessions.

Perhaps I'm making this far too complicated, any help would be much appreciated Thank you!!


You have to use session_start() in both scripts, the one setting the values (and presumably printing the <iframe>-element?) and the script that produces the contents for the iframe.

eg the "outer" script

<?php // test.php
session_start();
$_SESSION['productcheck'] = array();
$_SESSION['productcheck'][] = 'A';
$_SESSION['productcheck'][] = 'B';
$_SESSION['productcheck'][] = 'C';
session_write_close(); // optional
?>
<html>
  <head><title>session test</title></head>
  <body>
    <div>session test</div>
    <iframe src="test2.php" />
  </body>
</html>

and the script for the iframe content

<?php // test2.php
session_start();
?>
<html>
  <head><title>iframe session test</title></head>
  <body>
    <div>
      <?php
      if ( isset($_SESSION['productcheck']) && is_array($_SESSION['productcheck']) ) {
        foreach( $_SESSION['productcheck'] as $pc ) {
          echo $pc, "<br />n";
        }
      }
      ?>
    </div>
  </body>
</html>

Not sure what's up with your session variables, but you can definitely pass all four variables through the url in your iframe. You just need to separate your key value pairs with an ampersand. So something like this:

file.php?key1=val1&key2=val2&key3=val3 and so on.

This is probably a better way than using session variables if your just trying to get data into that other file.

链接地址: http://www.djcxy.com/p/59722.html

上一篇: AJAX不会携带会话变量

下一篇: 通过iframe,php传递会话变量