是否可以不设置$ _SERVER ['HTTP_USER_AGENT']? [英] Is it possible for $_SERVER['HTTP_USER_AGENT'] to not be set?

查看:183
本文介绍了是否可以不设置$ _SERVER ['HTTP_USER_AGENT']?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览网站的error_log,而已记录几次的错误之一是:

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

toro.php 中与之相关的行是:

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

是否可以通过HTTP请求设置$_SERVER['HTTP_USER_AGENT']?

Is it possible for $_SERVER['HTTP_USER_AGENT'] to not be set by a HTTP request?

推荐答案

是的,有可能,这是客户端发送(或不发送)的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:

当前请求中User-Agent:标头的内容

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

因此正确的代码应为:

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

这篇关于是否可以不设置$ _SERVER ['HTTP_USER_AGENT']?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆