Php check if in iframe

Check if site is inside iframe

PHP is never in an iframe. PHP is executed on the server side and generates output such as HTML, Javascript, or text. The output generated by PHP may produce or reside within an iframe, but never PHP itself.

ADDITIONAL DETAIL

With regard to the additional detail you added in comments (that you want to distinguish between requests directly to your site and requests via a Facebook application) there are a few techniques you can use:

You can check the referrer to determine if a request came from a Facebook URL, from another page on your own site, from a third-party site, or if it was direct traffic. This method is not foolproof, but may provide more information than your application currently receives.

You can create separate URLs for the application running on your site and the Facebook version. Using $_SERVER[‘REQUEST_URI’] , you can easily detect whether your application was accessed via ‘yoursite.com/fbapp’ or ‘yoursite.com/localapp’. Both URLs can reference the same scripts via Apache’s mod_rewrite or the aliasing solution of your choice.

This method is possibly the easiest to implement. If you can provide URL parameters when you provide an application URL to Facebook, just add a parameter. For example:

In PHP, you can easily check for the existence and value of the access_method parameter, and take action as necessary.

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' ) <> 

No, PHP is server side. It has no way of knowing if it is in a iframe.

Источник

Читайте также:  Send function as parameter php

Check If Site Is Inside Iframe

How to identify if a webpage is being loaded inside an iframe or directly into the browser window?

Browsers can block access to window.top due to same origin policy. IE bugs also take place. Here’s the working code:

function inIframe () try return window.self !== window.top; 
> catch (e) return true;
>
>

top and self are both window objects (along with parent ), so you’re seeing if your window is the top window.

Foolproof way to detect if this page is INSIDE a cross-domain iframe

First check if you are IFramed.

If you are IFramed, then your referrer is your parent frame url.

From this url you should be able to detect if you want to branch your code.

Check if site is inside iframe

PHP is never in an iframe. PHP is executed on the server side and generates output such as HTML, Javascript, or text. The output generated by PHP may produce or reside within an iframe, but never PHP itself.

ADDITIONAL DETAIL

With regard to the additional detail you added in comments (that you want to distinguish between requests directly to your site and requests via a Facebook application) there are a few techniques you can use:

You can check the referrer to determine if a request came from a Facebook URL, from another page on your own site, from a third-party site, or if it was direct traffic. This method is not foolproof, but may provide more information than your application currently receives.

You can create separate URLs for the application running on your site and the Facebook version. Using $_SERVER[‘REQUEST_URI’] , you can easily detect whether your application was accessed via ‘yoursite.com/fbapp’ or ‘yoursite.com/localapp’. Both URLs can reference the same scripts via Apache’s mod_rewrite or the aliasing solution of your choice.

This method is possibly the easiest to implement. If you can provide URL parameters when you provide an application URL to Facebook, just add a parameter. For example:

In PHP, you can easily check for the existence and value of the access_method parameter, and take action as necessary.

Detect iFrame embedding in Javascript

Looking at frame length breaks down generally if page A itself has frames (I know this might not be the case for this specific instance). The more reliable and meaningful test would be:

Читайте также:  What is buffer stream in java

PHP: Check if page is displayed inside iframe?

Php cannot tell the context for which the request is made. It doesn’t know anything except what is passed to it and available as server variable. You could add your own get parameter to the url to be used when putting your page in an iframe, but otherwise you are out of luck.

Источник

Php – Check if site is inside iframe

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?

Best Solution

PHP is never in an iframe. PHP is executed on the server side and generates output such as HTML, Javascript, or text. The output generated by PHP may produce or reside within an iframe, but never PHP itself.

ADDITIONAL DETAIL

With regard to the additional detail you added in comments (that you want to distinguish between requests directly to your site and requests via a Facebook application) there are a few techniques you can use:

You can check the referrer to determine if a request came from a Facebook URL, from another page on your own site, from a third-party site, or if it was direct traffic. This method is not foolproof, but may provide more information than your application currently receives.

You can create separate URLs for the application running on your site and the Facebook version. Using $_SERVER[‘REQUEST_URI’] , you can easily detect whether your application was accessed via ‘yoursite.com/fbapp’ or ‘yoursite.com/localapp’. Both URLs can reference the same scripts via Apache’s mod_rewrite or the aliasing solution of your choice.

This method is possibly the easiest to implement. If you can provide URL parameters when you provide an application URL to Facebook, just add a parameter. For example:

In PHP, you can easily check for the existence and value of the access_method parameter, and take action as necessary.

Источник

Detect if page is viewed inside an iframe in PHP

I have a blog service and I’m trying to prevent people from displaying the blogs on their own domains with an iframe.

Читайте также:  Вычисление выражений на php

I have added this code in my .htaccess :

Header always append X-Frame-Options SAMEORIGIN 

But I found out that even though modern browsers do not display the page’s content, the PHP script is still executing, as proven by adding this piece of code at the end of the script :

$file = fopen("test.txt","w"); fwrite($file,"script loaded"); 

Is there any way to detect that the page is being loaded in an iframe from PHP and stop the script’s execution in the first place ?

Answer

Solution:

There’s no way to detect if the content is being framed from a PHP script, you can only serve an X-Frame-Options header as you did to prevent the content being displayed by the browser.

This is often used as a security measure to prevent attacks such as clickjacking.

Share solution ↓

Additional Information:

Didn’t find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Similar questions

Find the answer in similar questions on our website.

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/

Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Источник

Оцените статью