Created
October 23, 2012 16:09
-
-
Save Abban/3939726 to your computer and use it in GitHub Desktop.
PHP Function to check Android version from the user agent.
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
/** | |
* Is Old Android | |
* | |
* Check to see it the user agent is Android and if so then | |
* check the version number to see if it is lower than 4.0.0 | |
* or passed parameter | |
* | |
* @param string $version | |
* @return boolean | |
*/ | |
function is_old_android($version = '4.0.0'){ | |
if(strstr($_SERVER['HTTP_USER_AGENT'], 'Android')){ | |
preg_match('/Android (\d+(?:\.\d+)+)[;)]/', $_SERVER['HTTP_USER_AGENT'], $matches); | |
return version_compare($matches[1], $version, '<='); | |
} | |
} | |
// usage | |
if(is_old_android()) echo ':( I am an old robot, qzzzzt'; | |
// or to check before any version | |
if(is_old_android('2.3.0')) echo ':( I am an even older robot, qzzzzt'; | |
// Pass that to your js in your template header | |
<script type="text/javascript"> | |
var old_android = <?php echo (is_old_android()) ? 'true' : 'false'; ?> | |
// Access it through the window global | |
console.log(window.old_android); | |
</script> |
This is quite the handy little tidbit. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
In my case
$_SERVER['HTTP_USER_AGENT']
is like thisApache-HttpClient/UNAVAILABLE (java 1.4)
Correct user agent is not being set. Not being detected Android.