2012-06-12 62 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

相關問題