1
votes

Is there a way to determine whether the user is using a web page in side and iframe or is it normal browsing using PHP?

3
or isnt there a way to do this?nivanka
it is a javascript, I want to know whether I can do it in PHPnivanka
If you'd've actually read the article you'd see that it's very hard to impossible in general, even using Javascript. It's most certainly impossible using PHP, since PHP has absolutely no idea what's going on on the client.deceze♦
my requirement is this; I have a site which has to view in two different ways when a user is viewing it in an iframe and viewing it normally. I know that I can pass a variable to do this, but I can only do it on the landing page unless i change every other link to have that url variable. Or else I can keep a session variable too, but if the user suddenly views the page outside of and iframe need to switch it to work for the normal browsing. what is the best way to do this? thank younivanka

3 Answers

2
votes

Using the javascript code that @deceze mentioned above (I pasted it in below),

if (parent.frames.length > 0) { ... }

If the above code noticed the page was displayed within iframe, then call 'IAmInIFRAME.php'(just example) via ajax call.

1
votes

You can add some GET parameters to the request while using IFRAME.

<iframe src="http://www.example.com/iframe?iframe=1">

But while non-iframe request there wouldn't be this GET parameter.

You can check is this GET parameter presents and define it in the session.

So there would be different sessions for iframe and usual window.

0
votes

The solution is to see if the parent's location and the current window's location is the same. If it is the same, then the page was loaded normally, if it is different then the page was loaded in an iframe.

var isInIFrame = (window.location != window.parent.location) ? true : false;

This came from this website. http://www.24hourapps.com/2009/01/check-if-page-is-loaded-in-iframe-using.html, and it came from the SO question here Check if site is inside iframe.

NOTE: In one test, I got a cross browser origin error but that would also only come if the two locations were different.