How to get parent window url without javascript?

I have an HTML file http://mydomain.com/page1.html:

<!doctype html> <html> <iframe src="http://mydomain.com/page2.php"></iframe> </html> 

In page2.php, I would like to get the URL of the "container page", which in this case is the string page1.html .

Is there any way to do this without javascript ?

+4
source share
5 answers

The browser can send the parent URL as a Referer in the HTTP request, so look at $_SERVER['HTTP_REFERER'] . This is not guaranteed. Thus, there is no 100% reliable method. If the client does not provide information in the request, there is nothing more that you can do on the server side, without an explicit parameter in the URL.

+7
source

Why not pass some unused GET parameter to the inner page?

 <iframe src="http://mydomain.com/page2.php?outerPageURL=<URL_ENCODED_CURRENT_URL>></iframe> 

Then you can make the inner page:

 $_GET['outerPageURL']; 
+4
source

You can simply use $_SERVER ['HTTP_REFERER'] for this.

+2
source

You can use $_SERVER['HTTP_REFERER'] , but sometimes this may not work. As far as I know, there is no other way without using javascript.

+2
source

You can use $ _ SERVER in PHP.

To get the url in your case base url :

 <?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?> 
+1
source

All Articles