6 Replies - 2219 Views - Last Post: 04 March 2012 - 08:08 PM

#1 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Get Browser & OS information

Posted 02 January 2010 - 02:15 AM

Description: this class is detecting visitor's (OS, Brwoser and Browser version)
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 "
".print_r($obj->showInfo("all"),true)."
"; // out put Array ( [0] => 3.6b5 [1] => Windows [2] => Firefox ) ?>


Is This A Good Question/Topic? 0
  • +

Replies To: Get Browser & OS information

#2 mshow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-October 09

Re: Get Browser & OS information

Posted 25 October 2010 - 04:28 AM

Great code! I used it and it worked. But can you make it to include additional os info like linux type e.g. ubuntu etc.
Was This Post Helpful? 0
  • +
  • -

#3 m.grigorov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-November 10

Re: Get Browser & OS information

Posted 17 November 2010 - 12:43 AM

Hey man how to make it possible to include in html file, you know ? Can you give me a example ?
Was This Post Helpful? 0
  • +
  • -

#4 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Get Browser & OS information

Posted 05 January 2011 - 02:26 AM

@m.grigorov: you can use java script....
Was This Post Helpful? 0
  • +
  • -

#5 JacksonD  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 215
  • Joined: 18-October 11

Re: Get Browser & OS information

Posted 21 October 2011 - 06:20 AM

sweet code.
Was This Post Helpful? 0
  • +
  • -

#6 cmptrzd1592  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 04-March 12

Re: Get Browser & OS information

Posted 04 March 2012 - 05:24 PM

I am getting an error on line 124. Unexpected '[', expecting ')'
Was This Post Helpful? 0
  • +
  • -

#7 cmptrzd1592  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 04-March 12

Re: Get Browser & OS information

Posted 04 March 2012 - 08:08 PM

I worked with this a little bit and I got it working. :) If you would like me to post it, let me know.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1