Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3: HTTP_USER_AGENT

I'm working on detecting the users agent for tech support on a site so that the user doesn't need to find it themselves. This is what I've got so far, but no matter what browser I test it on it shows up as the default "Unknown" Any Suggestions on where I went wrong?

def cyberbrowser
    mybrowser = ENV["HTTP_USER_AGENT"] 
       case mybrowser 
         when /MSIE 8.0/ then "Internet Explorer V8" 
         when /MSIE 7.0/ then "Internet Explorer V7" 
         when /MSIE 6.0/ then "Internet Explorer V6.0+" 
         when /MSIE 5.5/ then "Internet Explorer V5.5" 
         when /MSIE 5.22/ then "Internet Explorer V5.22" 
         when /MSIE 5.0/ then "Internet Explorer V5.0+" 
         when /MSIE 4.0/ then "Internet Explorer V4.0+" 
         when /MSIE 3.0/ then "Internet Explorer V3.0+" 
         when /MSIE 2.0/ then "Internet Explorer V2.0+" 
         when /Firefox/ then "Mozilla Firefox"
         when /Camino/ then "Camino"
         when /Dillo/ then "Dillo"
         when /Epiphany/ then "Epiphany"
         when /Firebird/ then "Mozilla Firebird"
         when /Thunderbird/ then "Mozilla Thunderbird"
         when /Galeon/ then "Mozilla Galeon"
         when /IBrowse/ then "IBrowse"
         when /iCab/ then "iCab"
         when /K-Meleon/ then "K-Meleon"
         when /Konqueror/ then "Konqueror"
         when /SeaMonkey/ then "SeaMonkey"
         when /Netscape/ then "Netscape"
         when /OmniWeb/ then "OmniWeb"
         when /Opera/ then "Opera"
         when /Safari/ then "Safari"
         else  "Unknown"  
       end
end
like image 860
mediarts Avatar asked Mar 08 '11 00:03

mediarts


2 Answers

Try request.env['HTTP_USER_AGENT'] in your controller. If this is in your model, pass it to your model.

like image 59
johnmcaliley Avatar answered Nov 04 '22 23:11

johnmcaliley


Also, checkout the UserAgent gem:

https://github.com/gshutler/useragent

like image 4
outside2344 Avatar answered Nov 04 '22 22:11

outside2344