通过iframe,php传递会话变量 [英] Passing sessions variables through an iframe, php

查看:97
本文介绍了通过iframe,php传递会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次到网站的定时器,不是一个过度经验的PHP程序员在这里:)

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

 
echo productcheck会话。$ _ SESSION ['productcheck']。
;
echoproductcheck1 session。$ _ SESSION ['productcheck1']。
;
echoproductcheck2会话。$ _ SESSION ['productcheck2']。
;
echoproductcheck3会话。$ _ SESSION ['productcheck3']。
;

这只是显示产品检查会话,每个会话后都没有任何内容,我将session变量设置为:

  $ _ SESSION ['productcheck'] = $ productBox; 

$ productBox是来自网址的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等等。不幸的是,我必须一次只接受一个变种,否则我只能通过所有4种产品,而不用担心会话。



也许我正在做这么远太复杂了,任何帮助都将非常感谢!

解决方案

您必须在两个脚本中使用session_start一个设置值(并且可能打印< iframe>元素?)和生成iframe内容的脚本。

例如。 外部脚本

 <?php // test.php 
session_start();
$ _SESSION ['productcheck'] = array();
$ _SESSION ['productcheck'] [] ='A';
$ _SESSION ['productcheck'] [] ='B';
$ _SESSION ['productcheck'] [] ='C';
session_write_close(); //可选
?>
< html>
< head>< title>会话测试< / title>< / head>
< body>
< div>会话测试< / div>
< iframe src =test2.php/>
< / body>
< / html>

以及iframe内容的脚本

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


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.

e.g. 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>

这篇关于通过iframe,php传递会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆