How cas i get the top URL from with a frame/iframe in PHP?

Posted on 2007-07-26
Last Modified: 2013-12-13
Hi All,

How cas i get the top URL from with a frame/iframe in PHP?

I thought $_SERVER['HTTP_HOST']  would get it, but it returns the child frame, not the parent?

many thanks.
Question by:detox1978
    LVL 24

    Expert Comment

    I ran a quick test, and it looks like the data that is returned is frame-specific. For example, I generated a file on my local test server that first outputted various attributes, and then used an iframe to include a script on a remote server that echoed the same attributes. The results are below:

    PHP_SELF: /test/iframe.php
    SERVER_NAME: localhost
    HTTP_HOST: localhost
    SCRIPT_FILENAME: /home/glcummins/dev/test/iframe.php

    (Contents of the iframe):

    PHP_SELF: /frame.php
    SCRIPT_FILENAME: /home/wwwhome/

    The test script I created looked like this:

    Outside the frame<br />
    echo "PHP_SELF: " . $_SERVER['PHP_SELF'] . "<br />";
    echo "SERVER_ADDR: " . $_SERVER['SERVER_ADDR'] . "<br />";
    echo "SERVER_NAME: " . $_SERVER['SERVER_NAME'] . "<br />";
    echo "HTTP_HOST: " . $_SERVER['HTTP_HOST'] . "<br />";
    echo "SCRIPT_FILENAME: " . $_SERVER['SCRIPT_FILENAME'] . "<br />";


    <iframe width="80%" src="">


    'frame.php' on the remote server just contained the echo statements. Try this kind of setup on your servers, and see what result you get.
    LVL 20

    Expert Comment

    php is a server site language and thus is not aware of parents of  frames , iframes popups etc.
    You can get the url of the parent using jacascript.

    <script language="javascript">
    LVL 2

    Author Comment

    i guess the best route would be to grab the URL using JavaScript and redirect the page to another page to collect it.

    Any idea how i could do this, i'm not that good with JavaScript
    LVL 20

    Accepted Solution

    Actually the best way to do it is not have the child frame getting the location of the parent, but rather having the parent frame storing its location.
    This can be done using sessions. Eg

    #### frame1.php  ########
    <? session_start();
    $_SESSION["path"] = $_SERVER['PHP_SELF'];
    <iframe src="frame2.php"></iframe>

    ##### frame2.php ##########
    <? session_start();
    print $_SESSION["path"];

    If for any reason you cant use sessions or prefer the javascript method tell me and ill give you an example.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now