简体   繁体   English

在iframe中获取内部父页面的网址

[英]get URL of parent page inside when inside an iframe

I have an iframe (iframe.php) inside my main page (index.php). 我的主页(index.php)中有一个iframe(iframe.php)。 I want to echo the URL of the main page inside the iframe. 我想在iframe中回显主页的URL。 I know how to echo the URL of any page but not when it is inside an iframe. 我知道如何回显任何页面的URL,但是当它在iframe中时却不。 When i try this, it is echoing only the URL of the iframe and not of the main page. 当我尝试此操作时,它仅回显iframe的URL,而不回显主页的URL。 It sounds simple but not able to figure it out. 听起来很简单,但无法弄清楚。

REASON : the reason why i chose php and why i am trying to do this is, i need to verify the URL from which this page is being accessed... do not want the iframe to be accessed unless the user is in index.php and reject the user from accessing it if he is not in the iframe of the main page. 原因 :我选择php以及尝试这样做的原因是,我需要验证从其访问此页面的URL ...除非用户位于index.php中,否则不希望访问iframe。如果用户不在主页的iframe中,则拒绝其访问。

You can use 您可以使用

$_SERVER["HTTP_REFERER"]

to read the parent. 阅读父母。

It work for me. 它对我有用。

The javascript code can't talk easily with PHP The ?from= javascript代码无法与PHP轻松对话。

PHP has no idea about an iframe, it's just served as another page and interpreted by the browser... PHP不了解iframe,它只是用作另一个页面并由浏览器解释...

You could do something like... 你可以做类似...

<iframe src="frame.php?from=<?php echo currentPageURL() ?>">

Then inside the iframe, you can access $_GET['from'] . 然后在iframe中,您可以访问$_GET['from'] Just make sure you verify, as that would be insecure. 只需确保您进行验证,因为那是不安全的。

Here is the code I typically use to return the current page URL: 这是我通常用于返回当前页面URL的代码:

function currentPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

Try this in iframe: 在iframe中尝试以下方法:

<script type="text/javascript">alert(top.location.href);</script>

I think it should work. 我认为应该可以。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM