通过iframe,PHP传递会话变量

1 php iframe get session-variables

第一次访问该网站,这里不是经验丰富的PHP程序员:)

我有一个问题,我正在网站中使用iframe,试图在其中使用会话变量,首先,我只是试图显示会话变量,以确保它们可以从iframe中访问:

    回声“productcheck的会议” $ _ SESSION [ 'productcheck']“。
“; 回声“productcheck1的会议” $ _ SESSION [ 'productcheck1']“。
“; 回声“productcheck2的会议” $ _ SESSION [ 'productcheck2']“。
“; 回声“productcheck3的会议” $ _ SESSION [ 'productcheck3']“。
“;

这仅显示了“产品检查会话”,每次都没有,我将会话变量设置为:

$_SESSION['productcheck'] = $productBox;
Run Code Online (Sandbox Code Playgroud)

$ productBox是来自URL的GET:

回声“ <iframe src = \” homeview.php?productBox = $ product1 \“ name = \” FRAMENAME \“ width = \” 594 \“ height = \” 450 \“ scrolling = \” No \“ id = \” FRAMENAME \“ allowautotransparency = \” true \“> </ iframe>”; 

奇怪的是,如果我只是使用从URL检索的$ productBox变量并使用该变量,那么代码就可以工作,只有当我将其存储在会话变量中时,它才会感到困惑。我想检索第二个$ productBox并将其分配给会话var productcheck1,依此类推。不幸的是,我必须一次接受一个var,否则我可以只通过所有4种产品而不必担心会话。

也许我把事情变得太复杂了,任何帮助将不胜感激,谢谢!

Vol*_*erK 5

您必须在两个脚本中都使用session_start(),一个用于设置值(并可能打印<iframe> -element?),另一个脚本会为iframe生成内容。

例如“外部”脚本

<?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>
Run Code Online (Sandbox Code Playgroud)

以及iframe内容的脚本

<?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>
Run Code Online (Sandbox Code Playgroud)