Prevent Embedding Web Page Inside An Iframe?

by Dmaind
5 replies
How to prevent my site page to be loaded via 3rd party site frame of iframe through php?
I do have javascript ready but I need a php solution.
#embedding #iframe #inside #page #prevent #web
  • Profile picture of the author tweakr
    This is a client-side thing so PHP won't be able to do anything with it. You'll have to stick with your JavaScript solution.
    {{ DiscussionBoard.errors[5457945].message }}
  • Profile picture of the author Dmaind
    Ok, but I think we can get domain name while loading the page through php. Although, I can't recall the function.

    If we can do that the rest all is simple.
    Signature
    372 Sold - WSO >>> Wicked Google Ranking Factors <<< Do You Know What Is Powerful Than HIGH PR .GOV LINKS?
    {{ DiscussionBoard.errors[5458217].message }}
  • Profile picture of the author ernestrategos
    Way of preventing your site from being 'taken' by other site. (URL Masking)

    First is the javascript solution, put this code in some part of your page:

    <script type="text/javascript">
    if(self != top) { top.location = self.location; }
    </script>

    (This will break the iframe call and 'put' you page url)

    (That's the most simple one, has it's pros and cons)
    for a extended list visit this site:

    http://www.willmaster.com/library/we...RL-masking.php
    Signature
    {{ DiscussionBoard.errors[5458314].message }}
    • Profile picture of the author wasabi20
      Originally Posted by ernestrategos View Post

      Way of preventing your site from being 'taken' by other site. (URL Masking)

      First is the javascript solution, put this code in some part of your page:

      <script type="text/javascript">
      if(self != top) { top.location = self.location; }
      </script>

      (This will break the iframe call and 'put' you page url)

      (That's the most simple one, has it's pros and cons)
      for a extended list visit this site:

      http://www.willmaster.com/library/we...RL-masking.php
      This is the best method.
      {{ DiscussionBoard.errors[5486958].message }}
  • Profile picture of the author kokopelli
    For a "PHP solution" that can do the trick, try something like:
    Code:
    <?php
    if($_SERVER['PHP_SELF'] != "/index.php") {
    echo '<script language="javascript" type="text/javascript">
    <!--
    	window.onload = function() {
    		if (top.location != location) {
    			top.location.href = document.location.href ;
    		}
    	}
    //-->
    </script>';
    }
    ?>
    Still basically Javascript, but cloaked in PHP. It won't stop all embeds, e.g. where visitors have Javascript turned off, or the new "super iframes" that Google Images and others use. To stop those, you have to go the "X-FRAME-OPTIONS: SAMEORIGIN" via HTTP headers route, which isn't a "frame-buster" per se, but will block sites that embed yours (i.e. visitors won't see anything embedded).
    Signature
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    {{ DiscussionBoard.errors[5488290].message }}

Trending Topics