How can I find out that my page is embedded as a frame to other site during page loading? I guess referrer request header can't help me here? Thanks.
相关问题
- Views base64 encoded blob in HTML with PHP
- how to define constructor for Python's new Nam
- Laravel Option Select - Default Issue
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
You cannot check it from the server's side, but you can use javascript to detect it after the page has loaded. Compare
top
andself
, if they're not identical, you are in a frame.Additionally, some modern browsers respect the
X-FRAME-OPTIONS
header, that can have two values:Users include Google's Picasa, that cannot be embedded in a frame.
Browsers that support the header, with the minimum version:
Or you can block a specific domain if you don't mind your content in some locations but don't want it on a certain site. For example, if
offendingdomain.com
was embedding your content, you could do this:This would check the parent document's location and see if it's the
offendingdomain.com
that is embedding your content. This script will then send that iframe to a certain famous youtube video as punishment. In effect they just Rick-Rolled themselves.For modern browsers, you can use CSP (Content Security Policy), which is a standard. The following header will prevent the document from loading in a frame anywhere:
(IE 11 needs the
X-
prefix, though). You can also change'none'
to the origin on which framing is allowed, such as your own site.To cover the older browsers, this is best used together with @Maerlyn's answer.
you can prevent loading you page in an iframe with javascript
this code change address of container of your page's iframe to your page address and force container to show your page.
Use javascript to check if it was loaded on iframe by placing the following script at the end of your php file and redirect to a page that displays warning or notice that your page should not be loaded using iframe.
Stackoverflow includes some JS to test it (
master.js
). This is the relevant part of it:But keep in mind that JS can be disabled.