<?php
class GetIPAddress {
private $_ip = null;
private $_result = null;
private $_arrResult = array();
private $_serverArr = array();
private $_key = null;
private $_ipAddress = null;
private $_userAgent = null;
private $_osPlatform = null;
private $_osArr = array();
private $_browser = null;
public function __construct() {
$this->_userAgent = $_SERVER['HTTP_USER_AGENT'];
}
public function userInfo() {
$this->_ip = $this->getServerTypes();
$this->_result = json_decode(file_get_contents("http://ip-api.com/" . $this->_ip . "/json"));
$this->getOS();
$this->_browser = $this->getBrowser($this->_userAgent);
if ($this->_result != null) {
$this->_arrResult = array($this->_result->status, $this->_result->country, $this->_result->countryCode,
$this->_result->region, $this->_result->regionName, $this->_result->city, $this->_result->zip,
$this->_result->lat, $this->_result->lon, $this->_result->timezone, $this->_result->isp,
$this->_result->org, $this->_result->as, $this->_result->query, $this->_osPlatform, $this->_browser);
return $this->_arrResult;
}
}
private function getServerTypes() {
$this->_serverArr = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
foreach ($this->_serverArr as $this->_key) {
if (array_key_exists($this->_key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$this->_key]) as $this->_ipAddress) {
$this->_ipAddress = trim($this->_ipAddress);
if (filter_var($this->_ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
return $this->_ipAddress;
}
}
}
}
}
private function getOS() {
$this->_osPlatform = 'Unknown OS Platform';
$this->_osArr = array(
'/windows nt 10/i' => 'Windows 10',
'/windows nt 6.3/i' => 'Windows 8.1',
'/windows nt 6.2/i' => 'Windows 8',
'/windows nt 6.1/i' => 'Windows 7',
'/windows nt 6.0/i' => 'Windows Vista',
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
'/windows nt 5.1/i' => 'Windows XP',
'/windows xp/i' => 'Windows XP',
'/windows nt 5.0/i' => 'Windows 2000',
'/windows me/i' => 'Windows ME',
'/win98/i' => 'Windows 98',
'/win95/i' => 'Windows 95',
'/win16/i' => 'Windows 3.11',
'/macintosh|mac os x/i' => 'Mac OS X',
'/mac_powerpc/i' => 'Mac OS 9',
'/linux/i' => 'Linux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/webos/i' => 'Mobile'
);
foreach ($this->_osArr as $key => $val) {
if (preg_match($key, $this->_userAgent)) {
$this->_osPlatform = $val;
}
}
return $this->_osPlatform;
}
private function getBrowser($useragent) {
// check for most popular browsers first
// unfortunately, that's IE. We also ignore Opera and Netscape 8
// because they sometimes send msie agent
if (strpos($useragent, 'MSIE') !== FALSE && strpos($useragent, 'Opera') === FALSE && strpos($useragent, 'Netscape') === FALSE) {
//deal with Blazer
if (preg_match("/Blazer\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
return 'Blazer ' . $matches[1];
}
//deal with IE
if (preg_match("/MSIE ([0-9]{1,2}\.[0-9]{1,2})/", $useragent, $matches)) {
return 'Internet Explorer ' . $matches[1];
}
} elseif (strpos($useragent, 'IEMobile') !== FALSE) {
if (preg_match("/IEMobile\/([0-9]{1,2}\.[0-9]{1,2})/", $useragent, $matches)) {
return 'Internet Explorer Mobile ' . $matches[1];
}
} elseif (strpos($useragent, 'Gecko')) {
//deal with Gecko based
//if firefox
if (preg_match("/Firefox\/([0-9]{1,2}\.[0-9]{1,2}(\.[0-9]{1,2})?)/", $useragent, $matches)) {
return 'Mozilla Firefox ' . $matches[1];
}
//if Netscape (based on gecko)
if (preg_match("/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
return 'Netscape ' . $matches[1];
}
//check chrome before safari because chrome agent contains both
if (preg_match("/Chrome\/([^\s]+)/", $useragent, $matches)) {
return 'Google Chrome ' . $matches[1];
}
//if Safari (based on gecko)
if (preg_match("/Safari\/([0-9]{2,3}(\.[0-9])?)/", $useragent, $matches)) {
return 'Safari ' . $matches[1];
}
//if Galeon (based on gecko)
if (preg_match("/Galeon\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
return 'Galeon ' . $matches[1];
}
//if Konqueror (based on gecko)
if (preg_match("/Konqueror\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
return 'Konqueror ' . $matches[1];
}
// if Fennec (based on gecko)
if (preg_match("/Fennec\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
return 'Fennec' . $matches[1];
}
// if Maemo (based on gecko)
if (preg_match("/Maemo\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
return 'Maemo' . $matches[1];
}
//no specific Gecko found
//return generic Gecko
return 'Gecko based';
} elseif (strpos($useragent, 'Opera') !== FALSE) {
//deal with Opera
if (preg_match("/Opera[\/ ]([0-9]{1}\.[0-9]{1}([0-9])?)/", $useragent, $matches)) {
return 'Opera ' . $matches[1];
}
} elseif (strpos($useragent, 'Lynx') !== FALSE) {
//deal with Lynx
if (preg_match("/Lynx\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
return 'Lynx ' . $matches[1];
}
} elseif (strpos($useragent, 'Netscape') !== FALSE) {
//NN8 with IE string
if (preg_match("/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
return 'Netscape ' . $matches[1];
}
} else {
//unrecognized, this should be less than 1% of browsers (not counting bots like google etc)!
return 'unknown';
}
}
}