我可以使用 PHP 获取 iframe 父窗口的 url 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14662731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I Get the url of iframe parent window with PHP?
提问by user2035347
I have an iframe on a webpage, and from the iframe i want to get the url of the parent window. Yes, i have been searching the web, but i keep getting solutions with JavaScript.
我在网页上有一个 iframe,我想从 iframe 中获取父窗口的 url。是的,我一直在网上搜索,但我一直在使用 JavaScript 获得解决方案。
So do your guys know any solutions that only uses PHP?
那么你们知道任何只使用 PHP 的解决方案吗?
回答by thordarson
A server-side script has no way of knowing anything about the client-side without the client side explicitly sending data, e.g. using AJAX.
如果客户端不显式发送数据,例如使用 AJAX,服务器端脚本无法了解客户端的任何信息。
If you have control over the iframe itself, you could always pass variables along in the query string and access them using $_GET:
如果您可以控制 iframe 本身,则始终可以在查询字符串中传递变量并使用$_GET以下命令访问它们:
HTML
HTML
<iframe src="http://example.com/file.php?variable=value"></iframe>
PHP
PHP
<?php
echo $_GET['variable'] // Outputs 'value'
?>
UPDATE (this might actually be possible)
更新(这实际上可能是可能的)
There might be a way using $_SERVER['HTTP_REFERER']. An HTML file containing an iframe sends some headers as it requests the file, and one of them appears to be the HTTP_REFERER. I tested this locally and it seems to work.
可能有一种方法使用$_SERVER['HTTP_REFERER']. 包含 iframe 的 HTML 文件在请求文件时会发送一些标头,其中之一似乎是 HTTP_REFERER。我在本地进行了测试,它似乎有效。
The only downside is you have no idea of knowing whether the referrer is an iframe or not. Again, if you have control over the iframe, you could pass a variable along using the method above saying it's an iframe, and use the method below to get the URL dynamically.
唯一的缺点是您不知道引用者是否是 iframe。同样,如果您可以控制 iframe,您可以使用上面的方法传递一个变量,说它是一个 iframe,并使用下面的方法动态获取 URL。
Example, let's say this file's URL is http://example.com/:
例如,假设这个文件的 URL 是http://example.com/:
<iframe src="http://example.com/file.php"></iframe>
The PHP file, we'll call it file.php:
PHP 文件,我们将其命名为 file.php:
<?php
echo $_SERVER['HTTP_REFERER']; // Should output http://example.com.
?>
回答by gary talman
I had the same problem, I was running a search routine within an iframe and wanted to be able to create links from other pages that would automate the search function of the said iframe.
我遇到了同样的问题,我在 iframe 中运行搜索例程,并希望能够从其他页面创建链接,以自动执行上述 iframe 的搜索功能。
After reading all the 'can only be done in javascript' I used this:
在阅读了所有“只能在 javascript 中完成”之后,我使用了这个:
$topurl=$_SERVER['HTTP_REFERER'];
$q=substr($topurl,strpos($topurl,'=')+1);
Crude but effective for my needs. I know you can get fancy and break out all the parameters of the url but I only needed one search term.
粗暴但有效满足我的需求。我知道你可以花哨并打破 url 的所有参数,但我只需要一个搜索词。
回答by Thijmen
That's not going to work, you could use JavaScript.
这是行不通的,您可以使用 JavaScript。

