繁体   English   中英

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

[英]check if page is viewed in browser? PHP

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

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

有任何想法吗?

谢谢

编辑:

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

多亏了安东尼·阿诺德,我想出了一个主意。 它不是很稳定,但现在应该这样做。

我只是在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();}

那就成功了。

检查User-Agent或使用获取浏览器功能检查哪个浏览器正在请求您的页面。 您可以将Web服务器配置为失败,除非指定了特定的用户代理。 您可以使用--user-agent开关在cURL中设置用户代理(请参见手册页 )。

不幸的是,用户代理可以被欺骗,因此您绝对不能绝对确定客户端发送的代理是正确的。

但是这个想法有一个问题。 如果它在公共网络上,您必须期望人们会尝试以任何方式访问它! 如果HTTP请求有效,则您的服务器将对其进行响应(在默认配置下)。 don't want it accessed by any method other than your prescribed cURL one, then you might need to invest in some further authentication/authorization methods (eg username/passphrase authentication via SSL). 如果您不希望通过规定的cURL以外的任何方法访问它,则可能需要投资一些其他的身份验证/授权方法(例如,通过SSL的用户名/密码身份验证)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM