问题
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?
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/7266107/best-way-to-determine-whether-a-webpage-is-viewed-in-an-iframe