Browser detection broken
-
I’m using Opera quite a bit on my site and found the plugin never detected Opera but instead classified it as “Unknown”.
The following code is in statistics.class.php:
public function get_UserAgent() { static $agent = null; if ( empty($agent) ) { $agent = $_SERVER['HTTP_USER_AGENT']; if ( stripos($agent, 'Firefox') ) { $agent = 'Firefox'; } elseif ( stripos($agent, 'MSIE') ) { $agent = 'IE'; } elseif ( stripos($agent, 'iPad') ) { $agent = 'Ipad'; } elseif ( stripos($agent, 'Android') ) { $agent = 'Android'; } elseif ( stripos($agent, 'Chrome') ) { $agent = 'Chrome'; } elseif ( stripos($agent, 'Opera') ) { $agent = 'Opera'; } elseif ( stripos($agent, 'Safari') ) { $agent = 'Safari'; } else { $agent = 'unknown'; } } return $agent; }
which is broken as stripos can return 0 which evals to false but is actually returning the fact the string was found at the first position in the string.
Also, the check for $agent being empty is redundent as it was just set to null the line before. A working version of the function is as follows:
public function get_UserAgent() { $agent = $_SERVER['HTTP_USER_AGENT']; if ( stripos($agent, 'Firefox') !== FALSE ) { $agent = 'Firefox'; } elseif ( stripos($agent, 'MSIE') !== FALSE ) { $agent = 'IE'; } elseif ( stripos($agent, 'iPad') !== FALSE ) { $agent = 'Ipad'; } elseif ( stripos($agent, 'Android') !== FALSE ) { $agent = 'Android'; } elseif ( stripos($agent, 'Chrome') !== FALSE ) { $agent = 'Chrome'; } elseif ( stripos($agent, 'Opera') !== FALSE ) { $agent = 'Opera'; } elseif ( stripos($agent, 'Safari') !== FALSE ) { $agent = 'Safari'; } else { $agent = 'unknown'; } return $agent; }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Browser detection broken’ is closed to new replies.