PHP / Javascript validation when working in a specific iframe

Is there a way to check PHP or Javascript (preferred PHP) if the current page is working in iFrame on a specific page. So, I would like something like this:

if(runningInIframeFromPage("http://www.myAwesomeWebsite.com")){ //go on with script } 

Is it possible?

+4
source share
2 answers

To find out if a page is in an iFrame, you can use this:

 if (window!=window.top) { /* iframe */ } 

So it's just a matter of checking with window.top.location.hostname (only the domain, not the whole URl. For the entire .href url)

 if (window!=window.top) { if(window.top.location.hostname = "www.myAwesomeWebsite.com" || window.top.location.hostname = "myAwesomeWebsite.com" ){ /* code if domain is myAwesomeWebsite.com or www.myAwesomeWebsite.com */ } } 
+2
source

Part of this question was answered before :

 if (parent==top) { // ... 

As for the PHP part - no, this is not possible, however you can always use JavaScript validation to disable the Ajax request to dynamically load the content you want.

+2
source

Source: https://habr.com/ru/post/1479622/


All Articles