Sanjay Rathod
Sanjay Rathod

Reputation: 1143

How to detect Android phone using php?

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

Answers (3)

Hossin Asaadi
Hossin Asaadi

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

Avatar
Avatar

Reputation: 15186

For PHP:

$isAndroid = (strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false);

Upvotes: 2

zur4ik
zur4ik

Reputation: 6254

You can detect Android user agent using PHP and Javascript also :

With PHP:

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if(stripos($ua,'android') !== false) { // && stripos($ua,'mobile') !== false) {
    echo "your message";
}

With JavaScript:

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
    // Do something!
    alert('Your message');
}

Upvotes: 32

Related Questions