h0mayun
h0mayun

Reputation: 3601

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 ?

Upvotes: 0

Views: 1751

Answers (1)

NPlay
NPlay

Reputation: 108

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.

Upvotes: 1

Related Questions