如何在 PHP 中将会话变量从一个域传递到另一个域
我遇到过一种情况,需要将 $_SESSION 变量从一个域传递到另一个域的 iFrame 页面。在过去的16天里,我尝试了各种方法,但没有效果。我认为唯一合乎逻辑的方法是对调用 iFrame 的 url 中的变量进行编码,并在 iFrame 页面中对它们进行解码。我不知道如何解决这个问题,我正在寻找我能找到的任何样品、帮助等。
感谢您的任何和所有建议。
这是我正在尝试做的一个示例...
示例:
<!-- Note only using hidden as I didn't want to build the form at test phase-->
<form name="test" method="post" action="iframe_test.php">
<input type="submit" name="Submit" />
<input type="hidden" name="fName" value="abc" />
<input type="hidden" name="lName" value="def" />
<input type="hidden" name="address1" value="ghi" />
<input type="hidden" name="address2" value="jkl" />
<input type="hidden" name="country" value="mno" />
<input type="hidden" name="postal_code" value="pqr" />
<input type="hidden" name="city" value="stu" />
<input type="hidden" name="retUrl" value="vwx">
<input type="hidden" name="decUrl" value="yz">
因此,从这里我将点击 iframe_test.php 并执行以下操作: PHP代码: 函数 StripSpecChar($val) { return (preg_replace('/[^a-zA-Z0-9" "-.@:/_]/','', $val)); 我
foreach ($_POST as $key => $val) {
$_SESSION[$key] = StripSpecChar($val);
}
得到一个如下所示的会话数组: 代码:
Array
(
[fName] => abc
[lName] => def
[address1] => ghi
[address2] => jkl
[country] => mno
[postal_code] => pqr
[city] => stu
[retUrl] => vwx
[decUrl] => yz
)
到目前为止一切都很好....调用 iFrame
代码:
<body>
Some page stuff here
<div align="center"><span class="style1"><strong>This is the iFrame Page</strong></span>
</div>
<div align="center">
<iframe src="https://www.other_domain.org/iframe/reserve.php" width="500" height="350" frameBorder="0"></iframe>
</div>
</body>
那么我该如何采取...
$_SESSION['fName']['abc'];
$_SESSION['lName']['def'];
$_SESSION['address1']['ghi'];
$_SESSION['address2']['jkl'];
$_SESSION['country']['mno'];
$_SESSION['postal_code']['pqr'];
$_SESSION['city']['stu'];
$_SESSION['retUrl']['vwx'];
$_SESSION['decUrl']['yz'];
并将其转换为我正在寻找的编码网址?此外,一旦完成,如何将会话变量恢复为新域 iFrame 页面上的会话变量...
I have encountered a situation where I need to pass $_SESSION variables from one domain to an iFrame page from another domain. I have spent the last 16 days trying various methods to no avail. I think that the only logical way would be to encode the variables in the url that calls the iFrame and decode them in th iFrame page. I am not sure how to go about this and I am looking for any samples, assistance etc that I can find.
Thanks for any and all suggestions.
Here is an example of what I am trying to do...
Example:
<!-- Note only using hidden as I didn't want to build the form at test phase-->
<form name="test" method="post" action="iframe_test.php">
<input type="submit" name="Submit" />
<input type="hidden" name="fName" value="abc" />
<input type="hidden" name="lName" value="def" />
<input type="hidden" name="address1" value="ghi" />
<input type="hidden" name="address2" value="jkl" />
<input type="hidden" name="country" value="mno" />
<input type="hidden" name="postal_code" value="pqr" />
<input type="hidden" name="city" value="stu" />
<input type="hidden" name="retUrl" value="vwx">
<input type="hidden" name="decUrl" value="yz">
So from here I am hitting the iframe_test.php and doing the following:
PHP Code:
function StripSpecChar($val) {
return (preg_replace('/[^a-zA-Z0-9" "-.@:/_]/','', $val));
}
foreach ($_POST as $key => $val) {
$_SESSION[$key] = StripSpecChar($val);
}
and I get a session array that looks like this:
Code:
Array
(
[fName] => abc
[lName] => def
[address1] => ghi
[address2] => jkl
[country] => mno
[postal_code] => pqr
[city] => stu
[retUrl] => vwx
[decUrl] => yz
)
Still all good so far....call the iFrame
Code:
<body>
Some page stuff here
<div align="center"><span class="style1"><strong>This is the iFrame Page</strong></span>
</div>
<div align="center">
<iframe src="https://www.other_domain.org/iframe/reserve.php" width="500" height="350" frameBorder="0"></iframe>
</div>
</body>
So HOW do I take...
$_SESSION['fName']['abc'];
$_SESSION['lName']['def'];
$_SESSION['address1']['ghi'];
$_SESSION['address2']['jkl'];
$_SESSION['country']['mno'];
$_SESSION['postal_code']['pqr'];
$_SESSION['city']['stu'];
$_SESSION['retUrl']['vwx'];
$_SESSION['decUrl']['yz'];
and turn it into the encoded url that I am looking for? Further once that is done how to I get the session vars back as session vars on that new domain iFrame page...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
序列化sessiondata数组并将其作为参数发送,然后反序列化
http://www.php.net/manual/en/function.serialize。 php
Serialize the sessiondata array and send it as a parameter and then deserialize it
http://www.php.net/manual/en/function.serialize.php
使用 serialize() 然后 base64_encode() 传递数据而不破坏数据并(主要)维护其结构。
这不是一个好的做法,因为任何弄清楚它是如何工作的人都可以注入任意数据,但如果这就是你想要做的,它就会起作用。
Use serialize() and then base64_encode() to pass the data without corrupting it and (mostly) maintaining its structure.
It's not a good practice, because then anyone who figures out how it works can inject arbitrary data, but if that's what you want to do, it will work.
为什么不直接将会话 ID 发送到其他域(并假设它们可以读取相同的会话存储基底)并使用它作为那里的会话 ID,例如
C.
Why not just send the session id to the otehr domain (and assuming they can read the same session storage substrate) use that as the session id there, e.g.
C.
您可以使用函数 获取关联数组并将其转换为查询字符串http_build_query
注意:您发布的第二个数组不是会话数组的正确输出。
在接收页面/域上,只需获取查询字符串并将预期参数放入您的 $_SESSION 数组中(或您需要对其执行的任何操作)。
这比使用序列化/反序列化之类的方法更安全,因为仅使用数组。
You can take an assoicative array and convert it to a query string with the function http_build_query
Note: the second array you posted is not the correct output of a session array.
On the receiving page/domain, just take the query string and place/sanitize the expected parameters into your $_SESSION array (or whatever you need to do with it).
This is safer than using something like serialize/unserialize as only arrays are being used.