Created
January 11, 2017 06:35
-
-
Save pingyen/6283f91c51a74017c314bc0de8e6fd6b to your computer and use it in GitHub Desktop.
iOS / Android Detection by 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
<?php | |
$platform = call_user_func(function() { | |
$token = $_SERVER['HTTP_USER_AGENT']; | |
if (isset($_GET['platform']) === true) { | |
$platform = $_GET['platform']; | |
return in_array($platform, array('iOS', 'Android', 'Other')) === true ? | |
$platform : | |
'Other'; | |
} | |
$pos = strrpos($token, 'Puffin/'); | |
if ($pos !== false) { | |
$token = substr($token, $pos + 7); | |
$len = strlen($token); | |
for ($i = 0; $i < $len; ++$i) { | |
switch ($token[$i]) { | |
case 'I': | |
return 'iOS'; | |
case 'A': | |
return 'Android'; | |
case ' ': | |
return 'Other'; | |
} | |
} | |
return 'Other'; | |
} | |
if (strpos($token, 'iPad') !== false || | |
strpos($token, 'iPhone') !== false || | |
strpos($token, 'iPod') !== false) { | |
return 'iOS'; | |
} | |
if (strpos($token, 'Android') !== false) { | |
return 'Android'; | |
} | |
return 'Other'; | |
}); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment