Can PHP detect if it is being displayed in an iFrame?

To detect if PHP is being displayed in an iFrame, you can check the HTTP_REFERER header to see if the request is coming from a different domain. If the request is coming from a different domain, it is likely being displayed in an iFrame. You can then take appropriate action based on this detection.

if(isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) === false){
    // Code to handle being displayed in an iFrame
    echo "This page is being displayed in an iFrame.";
} else {
    // Code to handle not being displayed in an iFrame
    echo "This page is not being displayed in an iFrame.";
}