php 是否可以不设置 $_SERVER['HTTP_USER_AGENT'] ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6465397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible for $_SERVER['HTTP_USER_AGENT'] to not be set?
提问by Martin Bean
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:
我刚刚浏览了一个网站error_log
,多次记录的错误之一是:
[21-Jun-2011 12:24:03] PHP Notice: Undefined index: HTTP_USER_AGENT in /home/ukevents/public_html/lib/toro.php on line 130
[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.phpis:
与toro.php相关的行是:
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?
是否有可能$_SERVER['HTTP_USER_AGENT']
不被 HTTP 请求设置?
回答by Maxim Krizhanovsky
Yes, it's possible, this a HTTP header sent (or not sent) by client, and you should not rely on it. From php manual:
是的,这是可能的,这是客户端发送(或不发送)的 HTTP 标头,您不应该依赖它。从 php 手册:
Contents of the User-Agent: header from the current request, if there is one
User-Agent 的内容:来自当前请求的标头,如果有
So the correct code would be:
所以正确的代码是:
private function ipad_request() {
return isset($_SERVER['HTTP_USER_AGENT']) && strstr($_SERVER['HTTP_USER_AGENT'], 'iPad');
}
回答by Andrew Moore
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.
是的。任何浏览器或用户代理都可以选择不发送User-Agent
标头。如果他们不发送该标头,$_SERVER['HTTP_USER_AGENT']
则不会设置。
Use isset()
to ensure that $_SERVER['HTTP_USER_AGENT']
is set.
使用isset()
以确保$_SERVER['HTTP_USER_AGENT']
已设置。
private function ipad_request() {
if(!isset($_SERVER['HTTP_USER_AGENT'])) return false;
return strstr($_SERVER['HTTP_USER_AGENT'], 'iPad') !== false;
}
回答by Guffa
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.
所有浏览器都会在请求中发送一个浏览器字符串,因此任何没有请求的请求都来自其他程序。即使所有行为良好的机器人都会发送浏览器字符串,因此您也不必担心不会出现在搜索引擎中。
回答by ChrisA
PHP docs says:
PHP 文档说:
'HTTP_USER_AGENT' Contents of the User-Agent: header from the current request, if there is one.
'HTTP_USER_AGENT' 用户代理的内容:来自当前请求的标头,如果有的话。
(relevant part italicised) so it would appear it might not always be set.
(相关部分斜体)所以看起来它可能并不总是被设置。
回答by Anono Mouser
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.
未定义 HTTP_USER_AGENT 的一个示例是,如果请求来自您网站的 GoDaddy 404 页面处理程序,您已将处理程序设置为您的页面之一。