Last active
August 6, 2017 22:09
-
-
Save sgraaf/923983e0227bc8c41b06d5bed62ed1e1 to your computer and use it in GitHub Desktop.
A collection of three useful HTTP_USER_AGENT functions for the client IP, OS and Browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// function to get the client ip address | |
function get_ip() { | |
$ipaddress = ''; | |
if ($_SERVER['HTTP_CLIENT_IP']) | |
$ipaddress = $_SERVER['HTTP_CLIENT_IP']; | |
else if($_SERVER['HTTP_X_FORWARDED_FOR']) | |
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
else if($_SERVER['HTTP_X_FORWARDED']) | |
$ipaddress = $_SERVER['HTTP_X_FORWARDED']; | |
else if($_SERVER['HTTP_FORWARDED_FOR']) | |
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; | |
else if($_SERVER['HTTP_FORWARDED']) | |
$ipaddress = $_SERVER['HTTP_FORWARDED']; | |
else if($_SERVER['REMOTE_ADDR']) | |
$ipaddress = $_SERVER['REMOTE_ADDR']; | |
else | |
$ipaddress = 'UNKNOWN'; | |
return $ipaddress; | |
} | |
// array of os's with their respective match in http_user_agent | |
$osList = array ( | |
/* -- WINDOWS -- */ | |
'Windows 10' => 'windows nt 10.0', | |
'Windows 8.1' => 'windows nt 6.3', | |
'Windows 8' => 'windows nt 6.2', | |
'Windows 7' => 'windows nt 6.1', | |
/* -- MAC OS X -- */ | |
'macOS High Sierra' => 'mac os x 10_13', | |
'macOS Sierra' => 'mac os x 10_12', | |
'Mac OS X El Capitan' => 'mac os x 10_11', | |
'Mac OS X Yosemite' => 'mac os x 10_10', | |
'Mac OS X Mavericks' => 'mac os x 10_9', | |
'Mac OS X Mountain Lion' => 'mac os x 10_8', | |
'Mac OS X (version unknown)' => 'mac os x', | |
/* -- MOBILE -- */ | |
'Android Nougat' => 'android 7', | |
'Android Marshmallow' => 'android 6', | |
'Android Lollipop' => 'android 5', | |
'Android KitKat' => 'android 4', | |
'Android (version unknown)' => 'android', | |
'iOS 11' => 'os 11', | |
'iOS 10' => 'os 10', | |
'iOS 9' => 'os 9', | |
'iOS 8' => 'os 8', | |
/* -- OTHERS -- */ | |
'Ubuntu' => 'ubuntu', | |
'Linux (or Linux based)' => 'linux' | |
); | |
// function to get the client os | |
function get_os_name() { | |
$user_agent = htmlspecialchars($_SERVER['HTTP_USER_AGENT']); | |
$user_agent = strtolower($user_agent); | |
global $osList; | |
foreach($osList as $os=>$match) | |
if (strpos($user_agent, $match)) | |
return $os; | |
return 'Other'; | |
} | |
// function to get the client browser | |
function get_browser_name() { | |
$user_agent = htmlspecialchars($_SERVER['HTTP_USER_AGENT']); | |
$user_agent = strtolower($user_agent); | |
if (strpos($user_agent, 'opera') || strpos($user_agent, 'opr/')) | |
return 'Opera'; | |
elseif (strpos($user_agent, 'edge')) | |
return 'Edge'; | |
elseif (strpos($user_agent, 'chrome')) | |
return 'Chrome'; | |
elseif (strpos($user_agent, 'safari')) | |
return 'Safari'; | |
elseif (strpos($user_agent, 'firefox')) | |
return 'Firefox'; | |
elseif (strpos($user_agent, 'msie') || strpos($user_agent, 'trident/7')) | |
return 'Internet Explorer'; | |
return 'Other'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With respect to the OS and Browser functions; they're by no means catch-all. If you want a bulletproof (or more detailed) solution, you should look into a user agent parser. This should, however, be able to provide you with some useful information about 99% of your visitors.