$_SERVER['HTTP_USER_AGENT'] 是否可以不设置?
我刚刚查看了网站的 error_log
,已记录几次的错误之一是:
[21-Jun-2011 12:24:03] PHP 注意:未定义索引:/home/ukevents/public_html/lib/toro.php 第 130 行中的 HTTP_USER_AGENT
该行属于toro.php 是:
private function ipad_request() {
return strstr($_SERVER['HTTP_USER_AGENT'], 'iPad');
}
$_SERVER['HTTP_USER_AGENT']
是否可以不由 HTTP 请求设置?
I've just been looking through a website's error_log
and one of the error's that has been logged a few times is:
[21-Jun-2011 12:24:03] PHP Notice: Undefined index: HTTP_USER_AGENT in /home/ukevents/public_html/lib/toro.php on line 130
The line this pertains to in toro.php is:
private function ipad_request() {
return strstr($_SERVER['HTTP_USER_AGENT'], 'iPad');
}
Is it possible for $_SERVER['HTTP_USER_AGENT']
to not be set by a HTTP request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,有可能,这是客户端发送(或不发送)的 HTTP 标头,您不应该依赖它。来自 PHP 手册:
那么正确的代码是:
Yes, it's possible, this a HTTP header sent (or not sent) by client, and you should not rely on it. From php manual:
So the correct code would be:
是的。任何浏览器或用户代理都可以选择不发送
User-Agent
标头。如果他们不发送该标头,则不会设置$_SERVER['HTTP_USER_AGENT']
。使用
isset()
确保$_SERVER['HTTP_USER_AGENT']< /code> 已设置。
Yes. Any browser or user-agent can choose not to send the
User-Agent
header. If they do not send that header,$_SERVER['HTTP_USER_AGENT']
won't be set.Use
isset()
to ensure that$_SERVER['HTTP_USER_AGENT']
is set.是的,这是可能的,但对于常规请求来说,这种情况永远不会发生。
所有浏览器都会在请求中发送浏览器字符串,因此任何没有浏览器字符串到达的请求都来自其他程序。即使所有行为良好的机器人都会发送浏览器字符串,因此您也不必担心不会出现在搜索引擎中。
Yes, it's possible, but it never happens for a regular request.
All browsers do send a browser string in the request, so any request that arrives without one comes from some other program. Even all well-behaving bots send a browser string, so you don't have to be concerned about not showing up in search engines either.
PHP 文档说:
“HTTP_USER_AGENT”
用户代理的内容:当前请求的标头,如果有。
(相关部分斜体)因此看起来它可能并不总是被设置。
PHP docs says:
'HTTP_USER_AGENT'
Contents of the User-Agent: header from the current request, if there is one.
(relevant part italicised) so it would appear it might not always be set.
HTTP_USER_AGENT 未定义的一个示例是,请求来自您网站的 GoDaddy 404 页面处理程序,您已将处理程序设置为您的页面之一。
An example where HTTP_USER_AGENT is undefined is if the request coming from GoDaddy's 404 page handler for your site where you have set the handler to be one of your pages.