4

Is there a way to check in either PHP or Javascript (preferrable PHP), if the current page is running in an iFrame on a specific page. So, I'd like something like this:

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

Is this possible?

Dirk
  • 2,094
  • 3
  • 25
  • 28
  • 3
    php does not know what an iframe is –  May 07 '13 at 21:01
  • 1
    at best php could check referrer, but that just means the current script is being executed due to a link at the refered site. there is NO way for server-side code to detect WHERE in a browser the page is loading: top level or iframe. – Marc B May 07 '13 at 21:02

2 Answers2

2

Part of this question has been answered before:

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

With regard to the PHP part - no, this is not possible, however you could always use the JavaScript check to fire off an Ajax request to dynamically load in the content you want.

Community
  • 1
  • 1
LeonardChallis
  • 7,759
  • 6
  • 45
  • 76
  • The only problem with your suggestion of using an ajax request to trigger the server side aspect is that the javascript and page have to already be rendered before the ajax request is triggered. That means your UI will load and then the next page load will see the server side aspect. – Joseph Crawford Jul 31 '15 at 18:18
2

To know if the 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 (just the domain, not entire URl. For entire URL is .href)

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  */
    }
}
RaphaelDDL
  • 4,452
  • 2
  • 32
  • 56