检查页面是否在浏览器中查看? PHP

时间:2011-11-20 23:58:58

标签: php curl

我想知道是否有任何方法可以检查页面是否已在PHP浏览器中运行(由人工)。

我有一个只需要通过cURL请求访问的页面。所以我不希望用户窥探它。

任何想法?

感谢

修改

因为这是在网络上不容易找到的问题之一,所以这是我使用的解决方案:

感谢anthony-arnold提出了一个想法。它不是很稳定,但现在应该这样做。

我只是在我的cURL请求中发送了用户代理:

//made a new var with the user agent string.
$user_agent = "anything I want in here, which will be my user agent";

//added this line in the cURL request to send the useragent to the target page:
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

然后我只是写了一个if语句来处理它:

if 
($_SERVER['HTTP_USER_AGENT'] == "my expected useragent - the string i previously placed into the $user_agent var."){

echo "the useragent is as expected, do whatever";
} 

else if 
($_SERVER['HTTP_USER_AGENT'] != "my expected useragent - the string i previously placed into the $user_agent var."){

echo "useragent is not as expected, bye now."; exit();}

这就是诀窍。

1 个答案:

答案 0 :(得分:4)

检查User-Agent或使用get browser function检查哪个浏览器正在请求您的信息页。除非指定了特定的用户代理,否则您可以将Web服务器配置为失败。您可以使用--user-agent开关在cURL中设置用户代理(请参阅man page)。

不幸的是,用户代理可能会被欺骗,因此您永远无法确定客户端发送的代理实际上是否正确。

但这个想法存在问题。如果它在公共网络上,你必须期望人们可能试图以任何方式访问它!如果HTTP请求有效,您的服务器将对其进行响应(在默认配置下)。如果您确实不希望通过除规定的cURL之外的任何方法访问它,那么您可能需要投资一些其他身份验证/授权方法(例如通过SSL进行用户名/密码验证)。 / p>

相关问题