我可以使用 PHP 获取 iframe 父窗口的 url 吗? [英] Can I Get the url of iframe parent window with PHP?

查看:62
本文介绍了我可以使用 PHP 获取 iframe 父窗口的 url 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网页上有一个 iframe,我想从 iframe 中获取父窗口的 url.是的,我一直在网上搜索,但我一直在使用 JavaScript 获得解决方案.

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.

那么你们知道任何只使用 PHP 的解决方案吗?

So do your guys know any solutions that only uses PHP?

推荐答案

如果客户端不显式发送数据,服务器端脚本无法了解客户端的任何信息,例如使用 AJAX.

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.

如果您可以控制 iframe 本身,您始终可以在查询字符串中传递变量并使用 $_GET 访问它们:

If you have control over the iframe itself, you could always pass variables along in the query string and access them using $_GET:

HTML

<iframe src="http://example.com/file.php?variable=value"></iframe>

PHP

<?php

    echo $_GET['variable'] // Outputs 'value'

?>

更新(这实际上是可能的)

可能有一种使用 $_SERVER['HTTP_REFERER'] 的方法.包含 iframe 的 HTML 文件在请求文件时会发送一些标头,其中之一似乎是 HTTP_REFERER.我在本地测试了这个,它似乎有效.

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.

唯一的缺点是您不知道引用者是否是 iframe.同样,如果您可以控制 iframe,您可以使用上面的方法传递一个变量,说它是一个 iframe,然后使用下面的方法动态获取 URL.

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.

例如,假设这个文件的 URL 是 http://example.com/:

<iframe src="http://example.com/file.php"></iframe>

PHP 文件,我们将其称为 file.php:

<?php
    echo $_SERVER['HTTP_REFERER']; // Should output http://example.com.
?>

这篇关于我可以使用 PHP 获取 iframe 父窗口的 url 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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