Check If Site Is Inside Iframe

How to identify if a webpage is being loaded inside an iframe or directly into the browser window?

Browsers can block access to window.top due to same origin policy. IE bugs also take place. Here's the working code:

function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}

top and self are both window objects (along with parent), so you're seeing if your window is the top window.

Foolproof way to detect if this page is INSIDE a cross-domain iframe

First check if you are IFramed.

window.self !== window.top

If you are IFramed, then your referrer is your parent frame url.

document.referrer

From this url you should be able to detect if you want to branch your code.

Check if site is inside iframe

PHP is never in an iframe. PHP is executed on the server side and generates output such as HTML, Javascript, or text. The output generated by PHP may produce or reside within an iframe, but never PHP itself.


ADDITIONAL DETAIL

With regard to the additional detail you added in comments (that you want to distinguish between requests directly to your site and requests via a Facebook application) there are a few techniques you can use:

  1. $_SERVER['HTTP_REFERER']:

You can check the referrer to determine if a request came from a Facebook URL, from another page on your own site, from a third-party site, or if it was direct traffic. This method is not foolproof, but may provide more information than your application currently receives.


  1. Separate URLs

You can create separate URLs for the application running on your site and the Facebook version. Using $_SERVER['REQUEST_URI'], you can easily detect whether your application was accessed via 'yoursite.com/fbapp' or 'yoursite.com/localapp'. Both URLs can reference the same scripts via Apache's mod_rewrite or the aliasing solution of your choice.


  1. URL Parameters

This method is possibly the easiest to implement. If you can provide URL parameters when you provide an application URL to Facebook, just add a parameter. For example:

?access_method=facebook

In PHP, you can easily check for the existence and value of the access_method parameter, and take action as necessary.

Detect iFrame embedding in Javascript

Looking at frame length breaks down generally if page A itself has frames (I know this might not be the case for this specific instance). The more reliable and meaningful test would be:

if (window!=window.top) { /* I'm in a frame! */ }

PHP: Check if page is displayed inside iframe?

Php cannot tell the context for which the request is made. It doesn't know anything except what is passed to it and available as server variable. You could add your own get parameter to the url to be used when putting your page in an iframe, but otherwise you are out of luck.



Related Topics



Leave a reply



Submit