Search code examples
phpiframex-frame-options

How to check if the URL is iframe embeddable in PHP?


Some websites are not allowed to be embedded via iframe. They produce the following error:

Refused to display 'https://news.ycombinator.com/news' in a frame because it 
set 'X-Frame-Options' to 'DENY'. 

Our app allows URL submissions from users. We want to check on the server side if the website could be embedded in iframe and add a corresponding flag. On the client we check for the flag, and either do iframe embed or just provide a direct link to a webpage.

How do I check whether website will support iframe or not?


Solution

  • X-Frame-Options is a response header sent by the server, so have your server perform an HTTP GET on the URL you'd like to test, see if the X-Frame-Options header is present, and if it is... judging by the spec you're not likely to be allowed to embed it at all.