繁体   English   中英

通过iframe,PHP传递会话变量

[英]Passing sessions variables through an iframe, php

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

我有一个问题,我正在网站中使用iframe,试图在其中使用会话变量,首先,我只是试图显示会话变量,以确保它们可以从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']."
";

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

$_SESSION['productcheck'] = $productBox;

$ productBox是来自URL的GET:

echo " <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种产品而不必担心会话。

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

您必须在两个脚本中都使用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>

以及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>

不确定会话变量是怎么回事,但是您肯定可以通过iframe中的url传递所有四个变量。 您只需要用&号分隔键值对即可。 所以像这样:

file.php?key1 = val1&key2 = val2&key3 = val3等。

如果您只是想将数据获取到另一个文件中,那么这可能比使用会话变量更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM