捕捉" X-Frame-Options禁止显示“

时间:2013-05-01 17:26:29

标签: javascript html iframe x-frame-options

我知道这个错误无法克服。

但我想要做的是,当我遇到一个无法嵌入的页面时,页面只是作为弹出窗口加载。目前正在发生的是我被重定向到页面。

对于无法嵌入的网页,我在chrome中看到以下错误。

 Refused to display 'http://www.nokia.com/us-en/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'

1 个答案:

答案 0 :(得分:2)

这是一个类似答案的链接,它提供了一个PHP脚本来检查标题: Detect X-Frame-Options

您可以对其进行修改,使其采用GET变量:

$error=false;
$urlhere=$_GET["url"];
$ch = curl_init();

$options = array(
        CURLOPT_URL            => $urlhere,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER         => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_ENCODING       => "",
        CURLOPT_AUTOREFERER    => true,
        CURLOPT_CONNECTTIMEOUT => 120,
        CURLOPT_TIMEOUT        => 120,
        CURLOPT_MAXREDIRS      => 10,
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch);
$headers=substr($response, 0, $httpCode['header_size']);
if(strpos($headers, 'X-Frame-Options: deny')>-1||strpos($headers, 'X-Frame-Options: SAMEORIGIN')>-1) {
        $error=true;
}
$httpcode= curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo json_encode(array('httpcode'=>$httpcode, 'error'=>$error));

然后使用ajax请求来测试每个URL

$.getJSON("/path/to/script.php?url="+url_variable, function (data) {
   if (data.error) { 
      // code to display pop-up
   } else { 
      // code to display iframe
   }
});