Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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';
}
like image 784
Sanjay Rathod Avatar asked Oct 17 '13 12:10

Sanjay Rathod


People also ask

How can I get browser information in PHP?

PHP get_browser() Function echo $_SERVER['HTTP_USER_AGENT']; $browser = get_browser(); print_r($browser);


2 Answers

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');
}
like image 135
zur4ik Avatar answered Sep 22 '22 00:09

zur4ik


For PHP:

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

like image 34
Avatar Avatar answered Sep 24 '22 00:09

Avatar