$_SERVER['HTTP_USER_AGENT'] 是否可以不设置?

发布于 2024-11-17 06:01:46 字数 404 浏览 0 评论 0原文

我刚刚查看了网站的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

盗梦空间 2024-11-24 06:01:46

是的,有可能,这是客户端发送(或不发送)的 HTTP 标头,您不应该依赖它。来自 PHP 手册:

用户代理的内容:当前请求的标头(如果有)

那么正确的代码是:

private function ipad_request() {
    return isset($_SERVER['HTTP_USER_AGENT']) && strstr($_SERVER['HTTP_USER_AGENT'], 'iPad');
}

Yes, it's possible, this a HTTP header sent (or not sent) by client, and you should not rely on it. From php manual:

Contents of the User-Agent: header from the current request, if there is one

So the correct code would be:

private function ipad_request() {
    return isset($_SERVER['HTTP_USER_AGENT']) && strstr($_SERVER['HTTP_USER_AGENT'], 'iPad');
}
放手` 2024-11-24 06:01:46

是的。任何浏览器或用户代理都可以选择不发送 User-Agent 标头。如果他们不发送该标头,则不会设置 $_SERVER['HTTP_USER_AGENT']

使用 isset() 确保 $_SERVER['HTTP_USER_AGENT']< /code> 已设置。

private function ipad_request() {
  if(!isset($_SERVER['HTTP_USER_AGENT'])) return false;

  return strstr($_SERVER['HTTP_USER_AGENT'], 'iPad') !== false;
}

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.

private function ipad_request() {
  if(!isset($_SERVER['HTTP_USER_AGENT'])) return false;

  return strstr($_SERVER['HTTP_USER_AGENT'], 'iPad') !== false;
}
梦明 2024-11-24 06:01:46

是的,这是可能的,但对于常规请求来说,这种情况永远不会发生。

所有浏览器都会在请求中发送浏览器字符串,因此任何没有浏览器字符串到达​​的请求都来自其他程序。即使所有行为良好的机器人都会发送浏览器字符串,因此您也不必担心不会出现在搜索引擎中。

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.

尸血腥色 2024-11-24 06:01:46

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.

傾城如夢未必闌珊 2024-11-24 06:01:46

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文