User’s browser and platform php class

Because I’ve been working in a drupal website, I tought it would be handy to learn some php so I can make some scripts for it, although I’m still a long long way from making a working drupal module. For now I’m using php code input format when I want to include some script.

As parte of my learning I made a class (which I’me really not using for anythin now) to detect user browser and platform. It uses HTTP_USER_AGENT to obtain the relevant information. Note that te code is mostly untested. I just tested Firefox and Chromium under Linux and Chrome and IE under Windows.

class Agent{
 	private $_browser;
 	private $_os;
 	private $_browsers = array("firefox", "msie", "chrome", "opera", "safari", "mozilla");
 	private $_OS = array("windows", "win32", "linux", "machintosh");
 
 	public function __Construct(){
 		//Get user agent
 		$info = $_SERVER['HTTP_USER_AGENT'];
 
 		//Lower string case for easier matches
 		$info = strtolower($info);
 		print $info."";
 
 		//Search for browser
 		foreach ($this->gt;_browsers as $browser){
 
 			 if (strpos($info, $browser) === false){
 			 	continue;
 			 }
 
 			 else{
 			 	$this->_browser = $browser;
 
 			 	//We need to change the name os IE from msie to IE
 			 	if ($this->_browser === "msie") $this->_browser = "Internet " .
 			 			"Explorer";
 
 			 	break;// We've found the browser, leave the loop
 			 }
 		}
 
 		//Search for OS
 		foreach ($this->gt;_OS as $os){
 
 			 if (strpos($info, $os) === false)
 			 	continue;
 			 else{
 			 	$this->_os = $os;
 
 			 	//If windows identifies as win32 we change it to Windows
 			 	if ($this->_os == "win32") $this->_os = "Windows";
 
 			 	break; //We've found our OS, leave loop
 			 }
 		}
 
 	//Make first letter of browser and OS uppercase
 	$this->_browser = ucfirst($this->_browser);
 	$this->_os = ucfirst($this->_os);
 
 	}
 
 	//Selector that returns browser type. The b in Browser is capitalized because of the
 	//library funtion get_browser
 	function get_Browser(){
 		return $this->_browser;
 	}
 
 	//Selector returns os
 	function get_os(){
 		return $this->_os;
 	}
 
 	//Browser modifier
 	function set_browser(string $browser){
 		$this->_browser = $browser;
 	}
 
 	//Os modifier
 	function set_os(string $os){
 		$this->_os = $os;
 	}
 
 	//Print browser and OS
 	function print_agent(){
 		print $this->_browser."@".$this->_os;
 	} 
 
 }


If you want to follow this post leave a comment bellow and continue the thread, or sbscribe the feed. If you don't have a feed reader you may subscribe by e-mail. Click here to sign up.

Ainda sem comentários.

Deixe o seu comentário

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

(obrigatório)

(obrigatório)