我如何确定一个页面是否加载在Facebook iframe使用PHP


How can I determine if a page is loaded in a Facebook iframe using PHP

我以为这很简单,但是我找不到任何相关的搜索结果。

我有一个页面标签在我的Facebook页面加载页面从我的服务器在一个iframe。我希望该页面只在Facebook请求时才提供服务。

我听说过查看用户代理,但这不起作用。至少用PHP…我想。

如果我必须,我会诉诸JavaScript重定向,但这只是草率。

理想情况下应该是这样的;

<?php
...
if ( ! $is_facebook )
{
    header("HTTP/1.1 404 Not Found");
}
?>

您可以检查http://developers.facebook.com/docs/authentication/signed_request/上描述的signed_request参数。如果您不需要太多的安全性,只需检查它的存在就足够了。如果你需要更多的确定性,你可以解码它来验证它真的来自Facebook。

你不想检查用户代理你想检查referrer但它不是完全可靠的

$_SERVER['HTTP_REFERER']
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.