A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

[PHP] User_Agent - Browser/Operating System Extractor

1.7k views · started by Chris ·
#1
[PHP] User_Agent - Browser/Operating System Extractor
<?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;
}
}
}
}
?>[/2][/1]


This is for you Personoid ;P
#2
Class is quite unnecessary for only two functions :p Regardless, looks to be working.
#3
Rofl. Nice timing :D i was thinking about this for some hardware i have thats programmable. Hard to explain. I can explain it later on MSN if you want.
#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?
$info = new ua;
$browser = $info->getBrowser();
$operating_sys = $info->getOPSystem();
#5
$info = new ua();
#6
Artificial wrote:
$info = new ua();


r u srs...?

Edit: didn't work as of yet. I'll mess with it more in a bit.
#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:

$info = new ua( $_SERVER['HTTP_USER_AGENT'] );
#8
I probably should have noticed that. I kept wondering what __construct was. :l
Thanks. I've never really worked with classes.
#9
O: I just started OO PHP. Should grasp it quickly, seeing as it isn't much different than C++.
#10
HTML wrote:
O: I just started OO PHP. Should grasp it quickly, seeing as it isn't much different than C++.


OOP ;] And yeah it's fun stuff.
#11
I just started Object Oriented Programming PHP
I just started Object Oriented PHP

No, his statement sounds better ;)