`
webdev2014
  • 浏览: 682321 次
文章分类
社区版块
存档分类
最新评论

Get Browser and Operating System Information with PHP

 
阅读更多
<?PHP
class OS_BR{

    private $agent = "";
    private $info = array();

    function __construct(){
        $this->agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : NULL;
        $this->getBrowser();
        $this->getOS();
    }

    function getBrowser(){
        $browser = array("Navigator"            => "/Navigator(.*)/i",
                         "Firefox"              => "/Firefox(.*)/i",
                         "Internet Explorer"    => "/MSIE(.*)/i",
                         "Google Chrome"        => "/chrome(.*)/i",
                         "MAXTHON"              => "/MAXTHON(.*)/i",
                         "Opera"                => "/Opera(.*)/i",
                         );
        foreach($browser as $key => $value){
            if(preg_match($value, $this->agent)){
                $this->info = array_merge($this->info,array("Browser" => $key));
                $this->info = array_merge($this->info,array(
                  "Version" => $this->getVersion($key, $value, $this->agent)));
                break;
            }else{
                $this->info = array_merge($this->info,array("Browser" => "UnKnown"));
                $this->info = array_merge($this->info,array("Version" => "UnKnown"));
            }
        }
        return $this->info['Browser'];
    }

    function getOS(){
        $OS = array("Windows"   =>   "/Windows/i",
                    "Linux"     =>   "/Linux/i",
                    "Unix"      =>   "/Unix/i",
                    "Mac"       =>   "/Mac/i"
                    );

        foreach($OS as $key => $value){
            if(preg_match($value, $this->agent)){
                $this->info = array_merge($this->info,array("Operating System" => $key));
                break;
            }
        }
        return $this->info['Operating System'];
    }

    function getVersion($browser, $search, $string){
        $browser = $this->info['Browser'];
        $version = "";
        $browser = strtolower($browser);
        preg_match_all($search,$string,$match);
        switch($browser){
            case "firefox": $version = str_replace("/","",$match[1][0]);
            break;

            case "internet explorer": $version = substr($match[1][0],0,4);
            break;

            case "opera": $version = str_replace("/","",substr($match[1][0],0,5));
            break;

            case "navigator": $version = substr($match[1][0],1,7);
            break;

            case "maxthon": $version = str_replace(")","",$match[1][0]);
            break;

            case "google chrome": $version = substr($match[1][0],1,10);
        }
        return $version;
    }

    function showInfo($switch){
        $switch = strtolower($switch);
        switch($switch){
            case "browser": return $this->info['Browser'];
            break;

            case "os": return $this->info['Operating System'];
            break;

            case "version": return $this->info['Version'];
            break;

            case "all" : return array($this->info["Version"], 
              $this->info['Operating System'], $this->info['Browser']);
            break;

            default: return "Unkonw";
            break;

        }
    }
}

// using
// create an new instant of OS_BR class
$obj = new OS_BR();
// // if you want to show one by one information then try showInfo() function

// get browser
echo $obj->showInfo('browser');

// get browser version
echo $obj->showInfo('version');

// get Operating system
echo $obj->showInfo('os');

// get all information and it returns an array
echo "<pre>".print_r($obj->showInfo("all"),true)."</pre>";
?>

原文:http://www.codeproject.com/Articles/545047/Get-Browser-and-Operating-System-with-PHP

分享到:
评论

相关推荐

    Practical Windows Forensics

    Tracking all these attacks and crimes requires a deep understanding of operating system operations, how to extract evident data from digital evidence, and the best usage of the digital forensic tools...

    Pro WPF in C# 2010 Mar 2010

    Microsoft's Windows Presentation Foundation (WPF) provides the foundation for building applications and high-quality user experiences for the Windows operating system. It blends the application user ...

    php.ini-development

    and you cannot use both "ob_gzhandler" and "zlib.output_compression". ; Note: output_handler must be empty if this is set 'On' !!!! ; Instead you must use zlib.output_handler. ; ...

    Intrusion Detection: Network Security beyond the Firewall

    Impact of SeOS on Base Operating System Security SeOS Auditing Other SeOS Features Going beyond SeOS Why You Still Need Intrusion Detection Chapter 4—Traditional Network Security Approaches ...

    a project model for the FreeBSD Project.7z

    The vision is “To produce the best UNIX-like operating system package possible, with due respect to the original software tools ideology as well as usability, performance and stability.” The ...

    IEradicator2001

    * Severe problems and Operating system Crashes can sometimes be solved by removing IE with IERadicator and then reinstalling IE again (if thats your choice) from the source files. (Such crashes are ...

    BURNINTEST--硬件检测工具

    data with random seeking" test mode and only occurs with some specific test settings. Release 5.3 build 1024 WIN32 release 10 July 2008 - Workaround for the rare crash bug in Vista in atklumdisp....

    Diskeeper 2008 v12.0.759.0

    PLATFORMS AND OPERATING SYSTEM VERSIONS SUPPORTED B. OVERVIEW OF DISKEEPER 2008 C. CHANGES DISKEEPER 2008 D. CHANGES IN THIS RELEASE E. KNOWN ISSUES IN THIS RELEASE F. KNOWN INCOMPATIBILITIES G. ...

    计算机网络第六版答案

    An Internet Exchange Points (IXP) (typically in a standalone building with its own switches) is a meeting point where multiple ISPs can connect and/or peer together. An ISP earns its money by ...

    VB编程资源大全(英文源码 控件)

    YOu can change the ToopTip and images in RunTime, this will make it easy to make a flashing icon to get the users attention.&lt;END&gt;&lt;br&gt;15 , ExcellOS.zip This is a fake Operating System &lt;END&gt;&lt;br&gt;16 ,...

    python3.6.5参考手册 chm

    PEP 492 - Coroutines with async and await syntax PEP 465 - A dedicated infix operator for matrix multiplication PEP 448 - Additional Unpacking Generalizations PEP 461 - percent formatting support ...

    .htaccess

    With most operating systems, though, all you need to do is to save the file by entering the name as: 创建.htaccess文件也许会给你带来一些困难。写文件很容易,你只需要在文字编缉器(例如:写字板)里写...

    pro_apache_third_edition..pdf

    Operating System Checklist.............................................................................33 Redundancy and Backup............................................................................

    Python Power - The Comprehensive Guide (2008).pdf

    Getting Information on Python ..................................................................................16 Python Communities .....................................................................

Global site tag (gtag.js) - Google Analytics