Skip to content

Instantly share code, notes, and snippets.

@bachors
Created December 30, 2015 09:13
Show Gist options
  • Save bachors/289e8cbc5f66186702c4 to your computer and use it in GitHub Desktop.
Save bachors/289e8cbc5f66186702c4 to your computer and use it in GitHub Desktop.
Visitor lookup using php
<?php
function user_agent($user_agent) {
$os_platform = "Unknown OS Platform";
$browser = "Unknown Browser";
$os_array = 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',
'android' => 'Android',
'blackberry' => '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'
);
$browser_array = array(
'OPR' => 'Opera',
'Flock' => 'Flock',
'Chrome' => 'Chrome',
'Opera.*?Version' => 'Opera',
'Opera' => 'Opera',
'MSIE' => 'Internet Explorer',
'Internet Explorer' => 'Internet Explorer',
'Trident.* rv' => 'Internet Explorer',
'Shiira' => 'Shiira',
'Firefox' => 'Firefox',
'Chimera' => 'Chimera',
'Phoenix' => 'Phoenix',
'Firebird' => 'Firebird',
'Camino' => 'Camino',
'Netscape' => 'Netscape',
'OmniWeb' => 'OmniWeb',
'Safari' => 'Safari',
'Konqueror' => 'Konqueror',
'icab' => 'iCab',
'Lynx' => 'Lynx',
'Links' => 'Links',
'hotjava' => 'HotJava',
'amaya' => 'Amaya',
'IBrowse' => 'IBrowse',
'Maxthon' => 'Maxthon',
'Ubuntu' => 'Ubuntu Web Browser'
);
foreach ($os_array as $regex => $value) {
if (preg_match('/'.$regex.'/i', $user_agent)) {
$os_platform = $value;
}
}
foreach ($browser_array as $regex => $value) {
if (preg_match('/'.$regex.'/i', $user_agent)) {
$browser = $value;
}
}
return $os_platform.'~'.$browser;
}
$user_agent = explode('~', user_agent($_SERVER['HTTP_USER_AGENT']));
$json = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
$user_lookup = json_decode($json);
$os = $user_agent[0];
$browser = $user_agent[1];
$ip = $user_lookup->ip;
$country = $user_lookup->country_name.' <img width="15" src="http://flagpedia.net/data/flags/small/'.strtolower($user_lookup->country_code).'.png">';
$region = $user_lookup->region_name;
$city = $user_lookup->city;
$time_zone = $user_lookup->time_zone;
$map = '<img width="500" src="https://maps.googleapis.com/maps/api/staticmap?center='.$user_lookup->latitude.','.$user_lookup->longitude.'&amp;zoom=9&amp;size=640x200&amp;sensor=false">';
$html = '<table><tr><td>IP</td><td>Browser</td><td>OS</td><td>Country</td><td>Region</td><td>City</td><td>Time Zone</td><td>Map</td></tr>';
$html .= '<tr><td>'.$ip.'</td><td>'.$browser.'</td><td>'.$os.'</td><td>'.$country.'</td><td>'.$region.'</td><td>'.$city.'</td><td>'.$time_zone.'</td><td>'.$map.'</td></tr></table>';
echo $html;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment