查找Web应用程序客户端的操作系统 [英] Find the operating system of web app's client

查看:165
本文介绍了查找Web应用程序客户端的操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我必须在其中查找我的Web应用程序用户的操作系统详细信息.如果用户在Windows上运行,则如果Linux为"Ubuntu..blah..blah或Fedora ...."(如果Debian是Debian的哪个"版本),则应输入"Windows 7 ... Blah blah".

I have a requirement where I have to find the operating system details of my web app's user. If the user is running on windows it should give "Windows 7 ... Blah blah " if Linux "Ubuntu.. blah..blah OR Fedora .. .. " if Debian "whichever" version of Debian.

谁能建议我们该怎么做?我看过 sys-uname gem.但这只是让我知道服务器端的信息.

Can anyone suggest how we can do it? I had a look at sys-uname gem. But it is letting me know just the server side information.

推荐答案

您必须使用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

编辑:创建另一个操作以确定操作系统

EDIT: Creating another action to determine the 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

希望有帮助!

这篇关于查找Web应用程序客户端的操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆