【问题标题】:Passing sessions variables through an iframe, php通过 iframe、php 传递会话变量
【发布时间】:2010-12-30 06:25:04
【问题描述】:

第一次来这个网站,不是经验丰富的 php 程序员 :)

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

echo "productcheck 会话".$_SESSION['productcheck']."
"; echo "productcheck1 的会话 ".$_SESSION['productcheck1']."
"; echo "productcheck2 的会话 ".$_SESSION['productcheck2']."
"; echo "productcheck3 会话".$_SESSION['productcheck3']."
";

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

$_SESSION['productcheck'] = $productBox;

$productBox 是来自 URL 的 GET:

echo " ";

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

也许我让这太复杂了,任何帮助将不胜感激谢谢!

【问题讨论】:

  • 奇怪,但我不明白这个问题。

标签: php iframe get session-variables


【解决方案1】:

您必须在两个脚本中使用 session_start(),一个设置值(并且可能打印

例如“外部”脚本

<?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>

【讨论】:

    【解决方案2】:

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

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

    如果您只是尝试将数据放入其他文件,这可能比使用会话变量更好。

    【讨论】:

      猜你喜欢
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      相关资源
      最近更新 更多