prevent page opening on an iframe?

拟墨画扇 提交于 2019-12-30 07:14:08

问题


How can I prevent page opening on an iframe using PHP? Like Gmail inside an iframe?


回答1:


For PHP, you can use.

header("X-FRAME-OPTIONS: DENY");

This is only supported in modern browsers, IE8+, Firefox 3.6.9, Safari 4 and Chrome. For older browser, you would also require some JavaScript.




回答2:


You can do that with JavaScript

if( (self.parent && !(self.parent===self))
    &&(self.parent.frames.length!=0)){
    self.parent.location=document.location
}

This will redirect from page that opens your page in iframe to your page.




回答3:


You cannot know from PHP code whether the current page is being loaded inside someone's iframe. PHP is run at the server, before anything is sent to the browser.

Only JavaScript, which runs in the browser, can detect if its window is the parent or not and do something about it.



来源:https://stackoverflow.com/questions/4948168/prevent-page-opening-on-an-iframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!