2

I have a page i only want to be visible if it is shown in a iframe.

Is this possible in PHP, htaccess or something? how?

I would prefer a solution in PHP

Thanks!

Gerben
  • 16,747
  • 6
  • 37
  • 56
2by
  • 1,083
  • 5
  • 22
  • 39

1 Answers1

9

Frames are client side only so you can't check it through PHP or .htaccess. You can check it in Javascript. See this thread:

How to identify if a webpage is being loaded inside an iframe or directly into the browser window?

EDIT Here's an example that would redirect for you. It's straight Javascript but if you were doing this with jQuery you could write the same function into document.ready

<script>
function check_frame() {
     if( top === self ) { // not in a frame
          location.href = "/link/to/some/url"; // either the frameset or an error.
     }
}
</script>
<body onLoad="check_frame()">
... normal code for your page here ...
</body>

Note that the page can still be a PHP page, it would just need to output that bit of Javascript.

Community
  • 1
  • 1
Cfreak
  • 19,191
  • 6
  • 49
  • 60
  • Ok, so Im not into Javascript at all, can you give me an example? – 2by Jan 17 '12 at 19:13
  • Why the need for document.ready? It's just plain JS, so will work with, or without jQuery. – Gerben Jan 17 '12 at 19:40
  • @Gerben - I just included that if he's already using jQuery it would make more sense to include that in document.ready instead of using onLoad – Cfreak Jan 17 '12 at 20:01
  • 1
    Missed that. There is however no need to wait for onload. You can just run the code directly, and avoid the delay. – Gerben Jan 17 '12 at 20:14