简体   繁体   中英

How to get URL of parent window without javascript?

I have a 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 anyway to do so without javascript ?

The browser may send the parent's URL as Referer in the HTTP request, so look at $_SERVER['HTTP_REFERER'] . That's not guaranteed though. As such, there's no 100% foolproof way. If the client doesn't supply the information in the request, there's nothing else whatsoever you can do on the server side without explicitly passing the parameter in the URL.

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

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

The inner page can then do:

$_GET['outerPageURL'];

你可以简单地使用$_SERVER ['HTTP_REFERER']

You can use $_SERVER['HTTP_REFERER'] , but it will may not work sometimes. There's no other way as far as I know, without using javascript.

You could use $_SERVER in PHP.

To get the URL in your case base url :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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