Skip to content

Instantly share code, notes, and snippets.

@Abban
Created October 23, 2012 16:09
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Abban/3939726 to your computer and use it in GitHub Desktop.
Save Abban/3939726 to your computer and use it in GitHub Desktop.
PHP Function to check Android version from the user agent.
/**
* 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>
@blackDelta
Copy link

Hi,
In my case $_SERVER['HTTP_USER_AGENT'] is like this Apache-HttpClient/UNAVAILABLE (java 1.4)
Correct user agent is not being set. Not being detected Android.

@thoughtspacewebsites
Copy link

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