Check if site is inside iframe

Viewed 60307

Anyone know if it's possible to check if a site is inside an iframe with PHP.

I know it's possible with javascript, but i couldn't find any example with PHP?

6 Answers

For anyone who lands on this old thread: There is a very easy way to check in PHP wether or not your page is loaded through an iframe.

if( isset($_SERVER['HTTP_SEC_FETCH_DEST']) && $_SERVER['HTTP_SEC_FETCH_DEST'] == 'iframe' ) {}

I find a workaround that can implement with php + javascript. On first url on your iframe set parameter iframe_view=true and next use this js to put on all links this url parameter again

<script>
$(function() {
   $("a").attr('href', function(index, item) {
     return item + (item.indexOf('?') != -1 ? "&iframe_view=true" : "?iframe_view=true");
   });
});
</script>

And then read it with php like:

if (isset($_GET['iframe_view'])) {
//iframe view
}
Related