WordPress.org

Ready to get started?Download WordPress

Forums

WP Statistics
[resolved] Browser detection broken (2 posts)

  1. Greg Ross
    Member
    Plugin Author

    Posted 9 months ago #

    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;
    		}

    http://wordpress.org/plugins/wp-statistics/

  2. Mostafa Soufi
    Member
    Plugin Author

    Posted 9 months ago #

    Was transferred to the email.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.