問題描述
任何人都知道是否可以使用 PHP 檢查網站是否在 iframe 內.
Anyone know if it's possible to check if a site is inside an iframe with PHP.
我知道使用 javascript 是可能的,但我找不到任何使用 PHP 的示例?
I know it's possible with javascript, but i couldn't find any example with PHP?
推薦答案
PHP 從不在 iframe 中.PHP 在服務器端執行并生成 HTML、Javascript 或文本等輸出.PHP 生成的輸出可能會產生或駐留在 iframe 中,但絕不會是 PHP 本身.
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.
其他細節
關于您在評論中添加的其他詳細信息(您想區分直接向您的網站發出的請求和通過 Facebook 應用程序發出的請求),您可以使用一些技巧:
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:
- $_SERVER['HTTP_REFERER']:
您可以檢查引薦來源網址以確定請求是來自 Facebook 網址、您自己網站上的另一個頁面、第三方網站還是直接流量.此方法并非萬無一失,但可能會提供比您的應用程序當前接收到的更多信息.
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.
- 單獨的網址
您可以為在您的網站上運行的應用程序和 Facebook 版本創建單獨的 URL.使用 $_SERVER['REQUEST_URI']
,您可以輕松檢測您的應用程序是通過yoursite.com/fbapp"還是yoursite.com/localapp"訪問的.兩個 URL 都可以通過 Apache 的 mod_rewrite 或您選擇的別名解決方案引用相同的腳本.
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.
- 網址參數
這個方法可能是最容易實現的.如果您在向 Facebook 提供應用程序 URL 時可以提供 URL 參數,只需添加一個參數.例如:
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:
?access_method=facebook
在 PHP 中,您可以輕松檢查 access_method
參數的存在和值,并根據需要采取行動.
In PHP, you can easily check for the existence and value of the access_method
parameter, and take action as necessary.
這篇關于檢查網站是否在 iframe 內的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!