简体   繁体   中英

Detect if page is viewed inside an iframe in PHP

I have a blog service and I'm trying to prevent people from displaying the blogs on their own domains with an iframe.

I have added this code in my .htaccess :

Header always append X-Frame-Options SAMEORIGIN

But I found out that even though modern browsers do not display the page's content, the PHP script is still executing, as proven by adding this piece of code at the end of the script :

$file = fopen("test.txt","w");
fwrite($file,"script loaded");

Is there any way to detect that the page is being loaded in an iframe from PHP and stop the script's execution in the first place ?

There's no way to detect if the content is being framed from a PHP script, you can only serve an X-Frame-Options header as you did to prevent the content being displayed by the browser.

This is often used as a security measure to prevent attacks such as clickjacking .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM