Check if site is inside iframe
Determining whether a PHP script is executing within an iframe context requires looking beyond standard server-side logic and understanding the HTTP headers exchanged between browsers, servers, and embedding documents. Unlike JavaScript, which has direct access to the window object and can inspect its ancestors via window.parent or event listeners, PHP runs on the server and lacks visibility into the client's DOM structure or window hierarchy. You cannot simply query a global variable to see if the current request is nested; instead, you must rely on specific HTTP response headers sent by the parent document.
The most reliable indicator that a page is being displayed inside an iframe is the X-Frame-Options header. When a browser loads a page inside an iframe, it enforces security policies defined by the embedding site to prevent clickjacking and other cross-site scripting attacks. If the parent site has set X-Frame-Options: SAMEORIGIN or X-Frame-Options: ALLOW-FROM, modern browsers will include a specific header in their requests to child frames indicating the origin of the parent document.
However, the header name varies depending on the browser and the version of the HTTP specification being followed. Historically, many browsers used the non-standard but widely supported X-Frame-Options header sent by the parent site. To detect this from within PHP, you need to inspect the incoming request headers for a value that indicates the presence of a parent frame.
In practice, you can check for the existence of the X-Frame-Options header in the _SERVER superglobal. If the header is present and contains a specific value, it implies the browser is enforcing a framing policy, which often happens when the page is embedded. A common pattern found in older frameworks or legacy browsers involves checking for HTTP_X_FRAME_OPTIONS.
$parentOrigin = $_SERVER['HTTP_X_FRAME_OPTIONS'] ?? null;
if ($parentOrigin) {
// The browser indicates it is running inside a frame
// with strict framing options set by the parent.
$inFrame = true;
} else {
// No framing header detected, likely rendering in a top-level window
// or the parent did not send specific headers.
$inFrame = false;
}
It is crucial to understand the limitations of this approach. The HTTP_X_FRAME_OPTIONS header is not part of the official HTTP/1.1 specification and is considered non-standard. Furthermore, modern browsers like Chrome and Firefox have deprecated reliance on this specific header name in favor of strict enforcement mechanisms that may not expose the parent's origin in a way that PHP can easily parse without risking breaking compatibility with newer browser versions.
Another significant constraint is cross-origin isolation. If your PHP script resides on a different domain than the parent iframe, you cannot directly read properties of the parent window due to the Same-Origin Policy. Even if a header suggests framing is active, you might not be able to interact with the parent document's content securely without setting up a CORS proxy or using postMessage for communication.
For modern applications built on frameworks like Laravel, checking for iframe context often involves verifying request headers in middleware before rendering sensitive views. If your application requires different behavior when embeddedâsuch as disabling certain UI elementsâyou can use this header detection to toggle logic dynamically. However, if you are trying to implement a "clickjacking protection" mechanism where the child page should refuse to load if not explicitly allowed by the parent, PHP alone cannot enforce this; the browser does that automatically based on the X-Frame-Options or Content-Security-Policy headers sent by your server.
If you require robust control over how your site behaves within frames without relying on deprecated non-standard headers, implementing a Content Security Policy (CSP) is the recommended approach. You can configure your web server to send a Content-Security-Policy header with frame-ancestors directives. While this restricts who can embed your page, it does not inherently tell your PHP script who is embedding it unless you inspect the incoming headers for the specific values provided by the browser during the framing negotiation.
Ultimately, detecting an iframe environment in PHP is a heuristic process based on optional HTTP headers rather than a definitive state check available to the server. Developers must weigh the reliability of these header inspections against the strictness of browser security implementations and ensure they do not rely solely on this method for critical access control decisions.