检查网站是否在 iframe 内
任何人都知道是否可以使用 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.
相关文章