php - Allow a page to only load in an iframe
907
How can I allow my PHP file to only load in aniframe
?
For example:
- Prevent direct access:
example.com/Loader.php
- Allow iframe access:
<iframe name="TEST" src="Example.com/Loader.php"></iframe>
Answer
Solution:
You wouldn't use PHP for that. Try javascript.
Answer
Solution:
Supposing that you have file
file1.php
that have aniframe
within and thisiframe
point tofile2.php
code supposed for the file file1.php:
content of the file file2.php:
So let update the file1.php to the following code :
and also update the file2.php as following :
Try this and tell me if this works or not :)
Answer
Solution:
I know this topic is old, but I was searching for a way to do this myself.
So perhaps others in the future will find this post seeking a pure php method of forcing a page to only load inside an iframe.
My solution is as follows:
If you control both pages (the one calling the iframe and the one loaded inside the iframe) it can be done.
First verify the referring page. If the referring page is not correct, kill the page.
As anyone can also spoof the HTTP_REFERER this is not an end all solution. So I use a load key that is stored in the database for each user of my site. On the page being iframed:
On the page that loads the iframe:
Finally on the iframe itself:
One could also use header redirects to the page loading the iframe instead of die;
The reason not to do this with javascript, is a user can still disable javascript and load the page causing any php on the page to still be run.