This is for you Personoid ;PPHP Code:
<?php
class ua {
private $Browsers = Array(
'firefox',
'msie',
'opera',
'chrome',
'safari',
'mozilla',
'seamonkey',
'konqueror',
'netscape',
'gecko',
'navigator',
'mosaic',
'lynx',
'amaya',
'omniweb',
'avant',
'camino',
'flock',
'aol'
);
private $OpSystems = Array(
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)|(Windows NT 6.1)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot' => '(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)'
);
private $Agent;
private $Browser = Array("Name" => null, "Version" => null);
private $OpSystem = null;
public function __construct($User_Agent) {
$this->Agent = $User_Agent;
}
public function getBrowser() {
if($this->Browser['Name'] != null && $this->Browser['Version'] != null) return $this->Browser;
for($i = 0, $size = sizeOf($this->Browsers); $i < $size; $i++) {
if(preg_match("#({$this->Browsers[$i]})[/ ]?([0-9.]*)#i", $this->Agent, $Match)) {
$this->Browser['Name'] = $Match[1];
$this->Browser['Version'] = $Match[2];
return $this->Browser;
}
}
}
public function getOpSystem() {
if($this->OpSystem != null) return $this->OpSystem;
foreach($this->OpSystems as $OpSystem => $Pattern) {
if(preg_match("/{$Pattern}/", $this->Agent, $t)) {
$this->OpSystem = $OpSystem;
return $this->OpSystem;
}
}
}
}
?>
Results 1 to 11 of 11
- 28 May. 2010 06:08am #1
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 0.00
[PHP] User_Agent - Browser/Operating System Extractor
- 28 May. 2010 01:17pm #2
Class is quite unnecessary for only two functions :p Regardless, looks to be working.
- 28 May. 2010 01:49pm #3
- 28 May. 2010 01:51pm #4
Thanks.
Also, I knew my user agent had Windows NT 6.1 but I wasn't sure if it was actually a Windows 7 user agent.
I thought maybe some other factors came into play since my shit's usually jacked up anyway.
Edit: what am I doing wrong with the class?
PHP Code:$info = new ua;
$browser = $info->getBrowser();
$operating_sys = $info->getOPSystem();
Last edited by Personoid; 28 May. 2010 at 02:36pm.
- 29 May. 2010 02:23am #5
$info = new ua();
- 29 May. 2010 04:25am #6
- 29 May. 2010 02:40pm #7
Lol, () actually isn't needed if there is no variables you need to pass through to the constructor method. Unfortunately, looking at that class, it requires a user agent in the constructor. In which case, the following would work:
PHP Code:$info = new ua( $_SERVER['HTTP_USER_AGENT'] );
- 29 May. 2010 03:58pm #8
I probably should have noticed that. I kept wondering what __construct was.
Thanks. I've never really worked with classes.
- 10 Jun. 2010 02:50am #9
O: I just started OO PHP. Should grasp it quickly, seeing as it isn't much different than C++.
- 09 Jul. 2010 06:04pm #10
- 09 Jul. 2010 09:43pm #11
I just started Object Oriented Programming PHP
I just started Object Oriented PHP
No, his statement sounds better