Overcoming “Display forbidden by X-Frame-Options”

后端 未结 26 2318
梦谈多话
梦谈多话 2020-11-21 06:31

I\'m writing a tiny webpage whose purpose is to frame a few other pages, simply to consolidate them into a single browser window for ease of viewing. A few of the pages I\'

26条回答
  •  Happy的楠姐
    2020-11-21 07:15

    Solution for loading an external website into an iFrame even tough the x-frame option is set to deny on the external website.

    If you want to load a other website into an iFrame and you get the Display forbidden by X-Frame-Options” error then you can actually overcome this by creating a server side proxy script.

    The src attribute of the iFrame could have an url looking like this: /proxy.php?url=https://www.example.com/page&key=somekey

    Then proxy.php would look something like:

    if (isValidRequest()) {
       echo file_get_contents($_GET['url']);
    }
    
    function isValidRequest() {
        return $_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['key']) && 
        $_GET['key'] === 'somekey';
    }
    

    This by passes the block, because it is just a GET request that might as wel have been a ordinary browser page visit.

    Be aware: You might want to improve the security in this script. Because hackers could start loading in webpages via your proxy script.

提交回复
热议问题