Organizational Research By

Surprising Reserch Topic

Question:PHP Undefined index: HTTP_USER_AGENT



asked Sep 13, 2013 in google-user-agent by anonymous
edited Sep 12, 2013
0 votes
427 views



Related Hot Questions

4 Answers

0 votes

 

he User-Agent header is optional. Firewalls may filter it or people may configure their clients to omit it. Simply check using isset() if it exists. Or even better, use !empty() as an empty header won't be useful either:
 
public static function detectBrowser() {
 if(empty($_SERVER['HTTP_USER_AGENT'])) {
 return array(
 'name' => 'unrecognized',
 'version' => 'unknown',
 'platform' => 'unrecognized',
 'userAgent' => ''
 );
 }
 
 // your old code here
}
However, since all of your code seems to work fine on an empty string and also yield the "unknown" values you could simply change the following line:
 
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
like this:
 
$userAgent = isset($_SERVER['HTTP_USER_AGENT'])
 ? strtolower($_SERVER['HTTP_USER_AGENT'])
 : '';
answered Sep 13, 2013 by anonymous
edited Sep 12, 2013
0 votes

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

 

 

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
 );
 }
answered Sep 13, 2013 by anonymous
edited Sep 12, 2013
0 votes

 

he User-Agent header is optional. Firewalls may filter it or people may configure their clients to omit it. Simply check using isset() if it exists. Or even better, use !empty() as an empty header won't be useful either:
 
public static function detectBrowser() {
 if(empty($_SERVER['HTTP_USER_AGENT'])) {
 return array(
 'name' => 'unrecognized',
 'version' => 'unknown',
 'platform' => 'unrecognized',
 'userAgent' => ''
 );
 }
 
 // your old code here
}
However, since all of your code seems to work fine on an empty string and also yield the "unknown" values you could simply change the following line:
 
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
like this:
 
$userAgent = isset($_SERVER['HTTP_USER_AGENT'])
 ? strtolower($_SERVER['HTTP_USER_AGENT'])
 : '';
answered Sep 13, 2013 by anonymous
edited Sep 12, 2013
0 votes

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

 

 

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
 );
 }
answered Sep 13, 2013 by anonymous
edited Sep 12, 2013

...