Reputation: 1143
If a user surfs to my site I want to display a message if he using Android phone. My website is http://website.com
How can I do this?
I have tried this but not succeeded:
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
// Do something!
// Redirect to Android-site?
window.location = 'http://android.davidwalsh.name';
}
Upvotes: 9
Views: 21299
Reputation: 405
function isAndroid() {
if(stripos($_SERVER['HTTP_USER_AGENT'],'android') !== false) {
return true;
}
return false;
}
Usage:
if(isAndroid()) {
echo "an android device";
} else {
echo "non-android device";
}
Upvotes: 1
Reputation: 15186
For PHP:
$isAndroid = (strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false);
Upvotes: 2
Reputation: 6254
You can detect Android user agent using PHP and Javascript also :
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if(stripos($ua,'android') !== false) { // && stripos($ua,'mobile') !== false) {
echo "your message";
}
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
// Do something!
alert('Your message');
}
Upvotes: 32