php httputils,PHP Undefined index: HTTP_USER_AGENT

The following code validates the user agent accessing the site however I am getting the error. What do I need to update to accommodate scenarios where there is no user agent being set?

ERROR

PHP Notice: Undefined index: HTTP_USER_AGENT in Utils.php on line 7

CODE

public static function detectBrowser()

{

$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);

if (preg_match('/opera/', $userAgent)) {

$name = 'opera';

}

elseif (preg_match('/webkit/', $userAgent)) {

$name = 'safari';

}

elseif (preg_match('/msie/', $userAgent)) {

$name = 'msie';

}

elseif (preg_match('/mozilla/', $userAgent) && !preg_match('/compatible/', $userAgent)) {

$name = 'mozilla';

}

else {

$name = 'unrecognized';

}

if (preg_match('/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches)) {

$version = $matches[1];

}

else {

$version = 'unknown';

}

if (preg_match('/linux/', $userAgent)) {

$platform = 'linux';

}

elseif (preg_match('/macintosh|mac os x/', $userAgent)) {

$platform = 'mac';

}

elseif (preg_match('/windows|win32/', $userAgent)) {

$platform = 'windows';

}

else {

$platform = 'unrecognized';

}

return array(

'name' => $name,

'version' => $version,

'platform' => $platform,

'userAgent' => $userAgent

);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值