21

I'm tring to embed a webpage in an iframe, but it doesn't work at all. internal pages with relative path are displayed normally. but this simple code doesn't work:

<iframe src="http://www.google.com/"></iframe>

the place that supposed to show the iframe is just empty. i looked in the page source and there is nothing after

How can this be?

Moshe Shaham
  • 14,666
  • 21
  • 68
  • 104

3 Answers3

35

Google uses an X-FRAME-OPTIONS HTTP header to disallow putting their pages in iframes: https://developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header

Almost all modern browsers will refuse to put pages with this HTTP header in an iframe. There's nothing you can do about that.

Daan
  • 3,343
  • 22
  • 19
3

Because the internal page had do something to prevent to be put in iframe.

Maybe a piece of javascript like that

if (window.top != window.self) {window.top.location = window.self.location;}
horsley
  • 470
  • 4
  • 10
-1

Suppose your url is www.google.com, i.e $url = "www.google.com";

$headerRes = get_headers($url);  //get the header response

foreach($headerRes as $val)
  if($val=="X-Frame-Options: SAMEORIGIN" || $val=="X-Frame-Options: DENY"){
    header("location:".$url); 
    exit; 
  }
//simply redirect to their website instead of showing blank frame

I hope I explained myself good.

Jan Doggen
  • 8,537
  • 13
  • 64
  • 132
Sachin Pundir
  • 185
  • 2
  • 5
  • 1
    Neither the question ask for a PHP solution nor does the answer indicate you are using a serverside solution (i.e. PHP) to redirect the user. – Ryan Schumacher Apr 14 '17 at 18:14