2012-06-12 64 views
1

我有一个要求,我必须找到我的Web应用程序用户的操作系统详细信息。如果用户在Windows上运行,它应该给予“Windows 7 ... Blah blah”如果Linux“Ubuntu .. blah..blah OR Fedora ..”,如果Debian“任何”版本的Debian。找到Web应用程序客户端的操作系统

任何人都可以建议我们如何做到这一点?我看了一下sys-uname宝石。但它让我知道服务器端信息。

+1

[Ruby的可能的重复 - 如何找出我的程序运行在哪个系统?](http://stackoverflow.com/questions/170956/ruby-how-can-i-find-out-on- which-system-my-program-is-running) –

+0

等一下,你问你访问你的应用程序的用户使用的是什么操作系统?或者你的应用程序实际运行的那个(服务器)? –

+0

使用'request.env ['HTTP_USER_AGENT']',@AndrewMarshall我想他在问客户端操作系统。不是你说的可能重复。 – uday

回答

3

你必须使用request.env['HTTP_USER_AGENT']

我发现了一个示例代码。

def getBrowser(bt) 
    rs=false 
    ua=request.env['HTTP_USER_AGENT'].downcase 
    isOpera = ua.index('opera') ? true : false 
    isSafari = (ua =~ /webkit|khtml/) ? true : false 
    isSafari3 = (ua.index('webkit/5')) ? true : false 
    isGecko = (!isSafari and ua.index('gecko')) ? true : false 
    isGecko3 = (!isSafari and ua.index('rv:1.9')) ? true : false 
    isIE = (!isOpera and ua.index('msie')) ? true : false 
    isIE7 = (!isOpera and ua.index('msie 7')) ? true : false 
    case bt 
     when 0 #isKonqueror 
     if ua.index('konqueror') then rs=true end 
     when 1 #isOpera 
     rs=isOpera 
     when 2 #isSafari 
     rs=isSafari 
     when 3 #isSafari2 
     rs=isSafari && !isSafari3 
     when 4 #isSafari3 
     rs=isSafari3 
     when 5 #isIE 
     rs=isIE 
     when 6 #isIE6 
     rs=isIE && !isIE7 
     when 7 #isIE7 
     rs=isIE7 
     when 8 #isGecko 
     rs=isGecko 
     when 9 #isGecko2 
     rs=isGecko && !isGecko3 
     when 10 #isGecko3 
     rs=isGecko3 
     when 11 #isWindows 
     if ua.index('windows') or ua.index('win32') then rs=true end 
     when 12 #isMac 
     if ua.index('macintosh') or ua.index('mac os x') then rs=true 
end 
     when 13 #isAir 
     if ua.index('adobeair') then rs=true end 
     when 14 #isLinux 
     if ua.index('linux') then rs=true end 
     when 15 #isSecure 
     s = request.env['SERVER_PROTOCOL'].downcase 
     if s.index('https') then rs=true end 
    end 
    rs 
    end 

编辑:创建另一个动作,以确定OS

def get_operating_system 
    if request.env['HTTP_USER_AGENT'].downcase.match(/mac/i) 
    "Mac" 
    elsif request.env['HTTP_USER_AGENT'].downcase.match(/windows/i) 
    "Windows" 
    elsif request.env['HTTP_USER_AGENT'].downcase.match(/linux/i) 
    "Linux" 
    elsif request.env['HTTP_USER_AGENT'].downcase.match(/unix/i) 
    "Unix" 
    else 
    "Unknown" 
    end 
end 

希望它能帮助!

+0

'bt'应该是什么?也有宝石可以做到这一点。 –

+0

是的,我真的想过给他一些代码。顺便说一句,你可以通过提及一些宝石增加更多的价值回答:) – uday

+0

@uDaY感谢您的答复。但是这只会给我一些浏览器细节。我试图捕获的详细信息是: 如果在Windows 7操作系统上访问应用程序,而不考虑浏览器。它应该给我'Windows 7家庭普通版/高级版/企业版 如果在Ubuntu上访问应用程序而不考虑浏览器,它应该给我'Ubuntu 11.04'等等。 所以它主要处理提取客户端/客户端机器的细节。 – AnkitG

相关问题