Click here to Skip to main content
Click here to Skip to main content

Get Browser and Operating System Information with PHP

By , 13 Feb 2013
Rate this:
Please Sign up or sign in to vote.

Get Browser and Operating System Information with PHP

Long time ago I needed to get browser information with PHP and I wrote a class which uses the preg_match_all function with regular expressions to get information from the browser. This class will return information from browser in an array format including the browser name, browser version, and also the operating system name.  It helped me so I shared to help others. 

Here is the complete code:

class OS_BR{

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

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

    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)));
                $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));
        return $this->info['Operating System'];

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

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

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

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

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

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

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

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

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

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

            default: return "Unkonw";


// 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>";

It is useful for getting browser name, browser version and operating system name. 

Any comments or suggestion would be appreciated.

The post Get Browser and Operating System Information with PHP appeared first on Recent Solutions.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Noor Ahmad Feroozi
Software Developer (Senior) Ministry of Education
Afghanistan Afghanistan
Over eight years of IT industry experience, and have been a part of design and development projects for many exceptional companies during those years, including Kabul Cyber Park, Alpha Omega Services, Ministry of Defense, Spacetech ICT Solutions, Ministry of Education and many others.
Technologies I use are standards-based XHTML, CSS, JavaScript, jQuery, PHP, ASP.Net, C#.Net, MS SQL, MySQL and SQLite. Each project I develop is built to be search engine and user friendly, using the latest development principles and techniques. To find out more about what I can do for you, check out my work at, or get follow me on http://twitter/naferoozi
Follow on   Twitter   Google+

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140421.2 | Last Updated 13 Feb 2013
Article Copyright 2013 by Noor Ahmad Feroozi
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid