[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-07-26
4
Medium Priority
?
2,803 Views
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.
D
0
Comment
Question by:detox1978
  • 2
4 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 19576500
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_ADDR: 127.0.0.1
SERVER_NAME: localhost
HTTP_HOST: localhost
SCRIPT_FILENAME: /home/glcummins/dev/test/iframe.php

(Contents of the iframe):

PHP_SELF: /frame.php
SERVER_ADDR: 192.168.11.4
SERVER_NAME: www.remote-server.com
HTTP_HOST: www.remote-server.com
SCRIPT_FILENAME: /home/wwwhome/www.remote-server.com/frame.php

The test script I created looked like this:

<html>
<body>
Outside the frame<br />
<?php
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="http://www.remote-server.com/frame.php">
</iframe>

</body>
</html>

'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.
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 19576644
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">
alert(parent.location.href);
</script>
0
 
LVL 2

Author Comment

by:detox1978
ID: 19577904
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
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 2000 total points
ID: 19594525
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'];
?>
<html>
<body>
<iframe src="frame2.php"></iframe>
</body>
</html>

##### 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.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 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