PHP Getting User Country Name, Browser, OS Name
Using PHP get user details IP address country, device, browser, operating system
Are you interested in extracting valuable information about your website visitors, such as their country, browser, and operating system? With PHP, you can easily obtain this data and enhance your website's functionality. By utilizing various PHP functions and APIs, you can retrieve the user's country based on their IP address, extract their browser information, and identify the operating system they are using. To get the user's country, you can leverage IP geolocation services or APIs, such as MaxMind or GeoIP. These services provide you with the user's geographical location based on their IP address, allowing you to personalize content or adjust settings accordingly. To determine the user's browser information, you can utilize the $_SERVER['HTTP_USER_AGENT'] variable and parse it using PHP's built-in functions. This information can help you optimize your website's design and functionality for different browsers. Lastly, to retrieve the operating system name, you can use the $_SERVER['HTTP_USER_AGENT'] variable and identify specific patterns or keywords that correspond to various operating systems. By implementing these PHP techniques, you can gain valuable insights about your website visitors and deliver a more tailored user experience.
<?php
/**
* User Agent details. user browser,OS,device,country
* @link https://www.html-code-generator.com/php/get-user-details
*/
class UserDetails {
private $agent = "";
private $browsers = array(
'SamsungBrowser'=> 'Samsung Browser',
'MiuiBrowser' => 'Xiaomi Browser',
'NokiaBrowser' => 'Nokia Browser',
'UCBrowser' => 'UC Browser',
'OPR' => 'Opera',
'Flock' => 'Flock',
'Edge' => 'Edge',
'edg' => 'Edge',
'Maxthon' => 'Maxthon',
'Opera.*?Version' => 'Opera',
'Opera' => 'Opera',
'MSIE' => 'Internet Explorer',
'Internet Explorer' => 'Internet Explorer',
'Trident.* rv' => 'Internet Explorer',
'YaBrowser' => 'Yandex Browser',
'Chrome' => 'Chrome',
'Shiira' => 'Shiira',
'Firefox' => 'Firefox',
'Chimera' => 'Chimera',
'Phoenix' => 'Phoenix',
'Firebird' => 'Firebird',
'Camino' => 'Camino',
'Netscape' => 'Netscape',
'OmniWeb' => 'OmniWeb',
'Safari' => 'Safari',
'Mozilla' => 'Mozilla',
'Konqueror' => 'Konqueror',
'icab' => 'iCab',
'Lynx' => 'Lynx',
'Links' => 'Links',
'hotjava' => 'HotJava',
'amaya' => 'Amaya',
'IBrowse' => 'IBrowse',
'Ubuntu' => 'Ubuntu Web Browser',
);
private $device_name = array(
'MI ' => 'MI',
'vivo' => 'Vivo',
'Ubuntu' => 'Ubuntu',
'S25' => 'Smart S-25',
'Moto G' => 'Moto G',
'LAVA' => 'LAVA',
'RMX' => 'Oppo Realme',
'Lenovo' => 'Lenovo',
'iPhone' => 'iPhone',
'pixel' => 'Google Pixel',
'BB' => 'BlackBerry',
'HUAWEI' => 'HUAWEI',
'LM' => 'LG',
'LGM' => 'LG',
'LG' => 'LG',
'windows phone' => 'Windows Phone',
'ASUS_' => 'Asus ZenFone',
'KFAPWI' => 'Kindle Fire',
'SM-' => 'Samsung',
'HONOR' => 'Huawei Honor',
'A37F' => 'Oppo',
'ONEPLUS' => 'One Plus',
'Karbonn' => 'Karbonn',
'XT' => 'Motorola',
'os x' => 'Apple Mac',
'mobileexplorer' => 'Mobile Explorer',
'palmsource' => 'Palm',
'palmscape' => 'Palmscape',
'motorola' => 'Motorola',
'nokia' => 'Nokia',
'nexus' => 'Nexus',
'palm' => 'Palm',
'ipad' => 'iPad',
'ipod' => 'Apple iPod Touch',
'sony' => 'Sony Ericsson',
'ericsson' => 'Sony Ericsson',
'blackberry' => 'BlackBerry',
'cocoon' => 'O2 Cocoon',
'blazer' => 'Treo',
'lg' => 'LG',
'amoi' => 'Amoi',
'xda' => 'XDA',
'mda' => 'MDA',
'vario' => 'Vario',
'htc' => 'HTC',
'samsung' => 'Samsung',
'sharp' => 'Sharp',
'sie-' => 'Siemens',
'alcatel' => 'Alcatel',
'benq' => 'BenQ',
'ipaq' => 'HP iPaq',
'mot-' => 'Motorola',
'playstation portable' => 'PlayStation Portable',
'playstation 3' => 'PlayStation 3',
'playstation vita' => 'PlayStation Vita',
'hiptop' => 'Danger Hiptop',
'nec-' => 'NEC',
'panasonic' => 'Panasonic',
'philips' => 'Philips',
'sagem' => 'Sagem',
'sanyo' => 'Sanyo',
'spv' => 'SPV',
'zte' => 'ZTE',
'sendo' => 'Sendo',
'nintendo dsi' => 'Nintendo DSi',
'nintendo ds' => 'Nintendo DS',
'nintendo 3ds' => 'Nintendo 3DS',
'wii' => 'Nintendo Wii',
'open web' => 'Open Web',
'openweb' => 'OpenWeb',
'android' => 'Android',
'symbian' => 'Symbian',
'SymbianOS' => 'SymbianOS',
'elaine' => 'Palm',
'series60' => 'Symbian S60',
'windows ce' => 'Windows CE',
'windows' => 'Windows',
'obigo' => 'Obigo',
'netfront' => 'Netfront Browser',
'openwave' => 'Openwave Browser',
'mobilexplorer' => 'Mobile Explorer',
'operamini' => 'Opera Mini',
'opera mini' => 'Opera Mini',
'opera mobi' => 'Opera Mobile',
'fennec' => 'Firefox Mobile',
'digital paths' => 'Digital Paths',
'avantgo' => 'AvantGo',
'xiino' => 'Xiino',
'novarra' => 'Novarra Transcoder',
'vodafone' => 'Vodafone',
'docomo' => 'NTT DoCoMo',
'o2' => 'O2',
'mobile' => 'Generic Mobile',
'wireless' => 'Generic Mobile',
'j2me' => 'Generic Mobile',
'midp' => 'Generic Mobile',
'cldc' => 'Generic Mobile',
'up.link' => 'Generic Mobile',
'up.browser' => 'Generic Mobile',
'smartphone' => 'Generic Mobile',
'cellphone' => 'Generic Mobile',
'Linux' => 'Linux'
);
private $os = array(
'windows nt 10.0' => 'Windows 10',
'windows nt 6.3' => 'Windows 8.1',
'windows nt 6.2' => 'Windows 8',
'windows nt 6.1' => 'Windows 7',
'windows nt 6.0' => 'Windows Vista',
'windows nt 5.2' => 'Windows 2003',
'windows nt 5.1' => 'Windows XP',
'windows nt 5.0' => 'Windows 2000',
'windows nt 4.0' => 'Windows NT 4.0',
'winnt4.0' => 'Windows NT 4.0',
'winnt 4.0' => 'Windows NT',
'winnt' => 'Windows NT',
'windows 98' => 'Windows 98',
'win98' => 'Windows 98',
'windows 95' => 'Windows 95',
'win95' => 'Windows 95',
'windows phone' => 'Windows Phone',
'windows' => 'Unknown Windows OS',
'android' => 'Android',
'blackberry' => 'BlackBerry',
'BB' => 'BlackBerry',
'iphone' => 'iOS',
'ipad' => 'iOS',
'ipod' => 'iOS',
'os x' => 'Mac OS X',
'ppc mac' => 'Power PC Mac',
'freebsd' => 'FreeBSD',
'ppc' => 'Macintosh',
'Linux' => 'Linux',
'debian' => 'Debian',
'sunos' => 'Sun Solaris',
'beos' => 'BeOS',
'apachebench' => 'ApacheBench',
'aix' => 'AIX',
'irix' => 'Irix',
'osf' => 'DEC OSF',
'hp-ux' => 'HP-UX',
'netbsd' => 'NetBSD',
'bsdi' => 'BSDi',
'openbsd' => 'OpenBSD',
'gnu' => 'GNU/Linux',
'unix' => 'Unknown Unix OS',
'symbian' => 'Symbian OS',
'Nokia' => 'Nokia Mobile',
);
function __construct() {
$this->agent = $_SERVER['HTTP_USER_AGENT'];
}
public function get_browser() {
$browser_name = "Unknown Browser";
foreach ($this->browsers as $key => $val) {
if (preg_match('|'.$key.'.|i', $this->agent, $match)) {
$browser_name = $val;
break;
}
}
return $browser_name;
}
public function get_os() {
$device_name = "Unknown Platform";
foreach ($this->os as $key => $val) {
if (preg_match('|'.preg_quote($key).'|i', $this->agent)) {
$device_name = $val;
break;
}
}
return $device_name;
}
public function get_device_name() {
$mobile = "unknown";
foreach ($this->device_name as $key => $val) {
if (FALSE !== (stripos($this->agent, $key))) {
$mobile = $val;
break;
}
}
return $mobile;
}
public function get_country() {
$output = array(
"city" => 'unknown',
"state" => '',
"country" => '',
"country_code" => '',
"continent" => '',
"continent_code" => ''
);
$ip = $this->get_ip();
$continents = array(
"AF" => "Africa",
"AN" => "Antarctica",
"AS" => "Asia",
"EU" => "Europe",
"OC" => "Australia (Oceania)",
"NA" => "North America",
"SA" => "South America"
);
if (filter_var($ip, FILTER_VALIDATE_IP)) {
$ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
$output = array(
"city" => @$ipdat->geoplugin_city,
"state" => @$ipdat->geoplugin_regionName,
"country" => @$ipdat->geoplugin_countryName,
"country_code" => @$ipdat->geoplugin_countryCode,
"continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
"continent_code" => @$ipdat->geoplugin_continentCode
);
}
}
return $output;
}
public function get_device() {
$tablet_browser = 0;
$mobile_browser = 0;
if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($this->agent))) {
$tablet_browser++;
}
if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i', strtolower($this->agent))) {
$mobile_browser++;
}
if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
$mobile_browser++;
}
$mobile_ua = strtolower(substr($this->agent, 0, 4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda ','xda-');
if (in_array($mobile_ua,$mobile_agents)) {
$mobile_browser++;
}
if (strpos(strtolower($this->agent),'opera mini') > 0) {
$mobile_browser++;
//Check for tablets on opera mini alternative headers
$stock_ua = strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])?$_SERVER['HTTP_X_OPERAMINI_PHONE_UA']:(isset($_SERVER['HTTP_DEVICE_STOCK_UA'])?$_SERVER['HTTP_DEVICE_STOCK_UA']:''));
if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
$tablet_browser++;
}
}
if ($tablet_browser > 0) {
return 'Tablet';
}
else if ($mobile_browser > 0) {
return 'Mobile';
}
else {
return 'Desktop';
}
}
public function get_ip() {
$mainIp = '';
if(getenv('REMOTE_ADDR'))
$mainIp = getenv('REMOTE_ADDR');
else
$mainIp = 'UNKNOWN';
return $mainIp;
}
}
?>
<?php
require './user-details.php';
$user_data = new UserDetails();
//user IP address
echo $user_data->get_ip();
// user browser
echo $user_data->get_browser();
//user Operating system
echo $user_data->get_os();
// user Device
echo $user_data->get_device();
//user Device name
echo $user_data->get_device_name();
//************** location *******************
// user location details
$location = $user_data->get_country();
// country
echo $location['country'];
// State
echo $location['state'];
// City
echo $location['city'];
// country code
echo $location['country_code'];
// Continent name
echo $location['continent'];
// Continent code
echo $location['continent_code'];
?>
<!DOCTYPE html>
<html>
<head>
<title>User Details</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
#myTable {
font-family: 'helvetica neue', helvetica, arial, 'lucida grande', sans-serif;
border-collapse: collapse;
}
#myTable td {
border: solid 1px #e4e4e4;
padding: 10px;
font-size: 15px;
text-align: left;
font-weight: bold;
color: #7d7d7d;
}
#myTable td>div {
font-weight: normal;
}
#myTable tr {
background-color: #fbfbfb;
color: #000000;
}
#myTable tr:hover {
background-color: #FFFFFF;
color: #000000;
}
</style>
<body>
<?php
require './user-details.php';
$user_data = new UserDetails();
$location = $user_data->get_country();
echo '<table id="myTable" width="100%">
<tbody>
<tr>
<td>Country</td>
<td><div>'.$location['country'].'</div></td>
</tr>
<tr>
<td>State</td>
<td><div>'.$location['state'].'</div></td>
</tr>
<tr>
<td>City</td>
<td><div>'.$location['city'].'</div></td>
</tr>
<tr>
<td>country code</td>
<td><div>'.$location['country_code'].'</div></td>
</tr>
<tr>
<td>Continent</td>
<td><div>'.$location['continent'].'</div></td>
</tr>
<tr>
<td>Continent code</td>
<td><div>'.$location['continent_code'].'</div></td>
</tr>
<tr>
<td>IP Address</td>
<td><div>'.$user_data->get_ip().'</div></td>
</tr>
<tr>
<td>Browser</td>
<td><div>'.$user_data->get_browser().'</div></td>
</tr>
<tr>
<td>Operating system</td>
<td><div>'.$user_data->get_os().'</div></td>
</tr>
<tr>
<td>Device</td>
<td><div>'.$user_data->get_device().'</div></td>
</tr>
<tr>
<td>Device name</td>
<td><div>'.$user_data->get_device_name().'</div></td>
</tr>
</tbody>
</table>';
?>
</body>
</html>
-
user-details
- index.php
- user-details.php