How can PHP prevent certain page elements from being displayed when loaded in an iFrame?

To prevent certain page elements from being displayed when loaded in an iFrame, you can use PHP to check if the page is being loaded in an iFrame and then conditionally hide the elements. This can be achieved by checking the HTTP_REFERER header to see if the request is coming from an iFrame and then using PHP to output the appropriate HTML code to hide the elements.

<?php
if(isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'example.com') !== false) {
    // This page is being loaded in an iFrame, hide the elements
    echo '<style>.iframe-element { display: none; }</style>';
}
?>