最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - get URL of parent page inside when inside an iframe - Stack Overflow

programmeradmin1浏览0评论

I have an iframe (iframe.php) inside my main page (index.php). I want to echo the URL of the main page inside the iframe. I know how to echo the URL of any page but not when it is inside an iframe. When i try this, it is echoing only the URL of the iframe and not of the main page. 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.

I have an iframe (iframe.php) inside my main page (index.php). I want to echo the URL of the main page inside the iframe. I know how to echo the URL of any page but not when it is inside an iframe. When i try this, it is echoing only the URL of the iframe and not of the main page. 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.

Share Improve this question edited Feb 22, 2011 at 18:05 Scorpion King asked Feb 22, 2011 at 17:48 Scorpion KingScorpion King 1,9389 gold badges38 silver badges45 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

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=

PHP has no idea about an iframe, it's just served as another page and interpreted by the browser...

You could do something like...

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

Then inside the iframe, you can access $_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:

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:

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

I think it should work. :-)

发布评论

评论列表(0)

  1. 暂无评论