skip to Main Content

How to check whether a web page can be loaded in iframe

How To Check Whether A Web Page Can Be Loaded In Iframe

Sometimes you want to load other web page in your website’s iframe, but due to security concerns, other website may have security configuration that prevent your website from loading their pages in to the iframe. In this case, if you want to load them into your iframe, you will see the error like screenshot below:

image.png

To prevent a page from loading in iframe, the response header will send to the browser with option which denies the load. This already talked in this topic How to block website from loading in iframe.

The below PHP script will tell you if the website allow to load in iframe:

function isIframeDisabled($src){
    try{
        $headers = get_headers($src, 1);
        $headers = array_change_key_case($headers, CASE_LOWER);
        // Check Content-Security-Policy
        if(isset($headers[strtolower('Content-Security-Policy')])){
            return true;
        }
        // Check X-Frame-Options
        if(isset($headers[strtolower('X-Frame-Options')] &&
           (strtoupper($headers['X-Frame-Options']) == 'DENY' ||
            strtoupper($headers['X-Frame-Options']) == 'SAMEORIGIN')
        ){
            return true;
        }
    } catch (Exception $ex) {
        // Ignore error
    }
    return false;
}

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
Search