How to detect mobile browser


Most WAP-enabled devices only have a viewing area of 150×150. You can only reliably detect if they are a WAP enabled device by using a server-side language to check to see if the device accepts certain things, things that are unique to wireless devices.

Basically you check to see if the browser accepts text/vnd.wap.wml. If it does accept this, then it is a WAP enabled device, and thus a handheld.

You would also probably want to check if it is a pocket PC, so then would check if the HTTP_USER_AGENT is Windows CE. If it is, than again, you have a WAP enabled device.

You cannot use JavaScript to do this, as WAP devices do not offer full (and sometimes no) support of scripting blocks or advanced features. It will often simply not parse them.

So in PHP you would use this:


$user_agent = $_SERVER['HTTP_USER_AGENT'];
$accept = $_SERVER['HTTP_ACCEPT'];

switch(true){

case (preg_match(‘/ipad/i’,$user_agent)):
//echo ‘IPAD’;
break;

case (preg_match(‘/ipod/i’,$user_agent)||preg_match(‘/iphone/i’,$user_agent)):
//echo ‘IPHONE’;
break;

case (preg_match(‘/android/i’,$user_agent)):
//echo ‘ANDROID’;
break;

case (preg_match(‘/opera mini/i’,$user_agent)):
//echo ‘OPERA MINI’;
break;

case (preg_match(‘/blackberry/i’,$user_agent)):
//echo ‘BLACKBERRY’;
break;

case (preg_match(‘/(pre\/|palm os|palm|hiptop|avantgo|plucker|

xiino|blazer|elaine)/i’,$user_agent)):

//echo ‘PALM OS’;

break;

case (preg_match(‘/(iris|3g_t|windows ce|opera mobi|windows ce;

smartphone;|windows ce; iemobile)/i’,$user_agent)):

//echo ‘WINDOWS CE’;

break;

case (preg_match(‘/(mini 9.5|vx1000|lge |m800|e860|u940|ux840|compal|

wireless| mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|

sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|

samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|

s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|

471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|
w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|fly v71|s302|

-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|

n120|mtk |c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|

s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|

bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|

mw200|brew |d88|htc\/|htc_touch|355x|m50|km100|d736|p-9521|telco|

sl74|ktouch|m4u\/|me702|8325rc|kddi|phone|lg |sonyericsson|samsung|

240x|x320|vx10|nokia|sony cmd|motorola|up.browser|up.link|mmp|

symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|

psp|treo)/i’,$user_agent)):
//echo ‘MOTOROLA’;
break;

case ((strpos($accept,’text/vnd.wap.wml’)>0)||

(strpos($accept,’application/vnd.wap.xhtml+xml’)>0)):
//echo ‘WAP BROWSER’;
break;

case (isset($_SERVER[‘HTTP_X_WAP_PROFILE’])||isset($_SERVER[‘HTTP_PROFILE’])):
//echo ‘WAP BROWSER’;
break;

case (isset($_SERVER[‘HTTP_X_WAP_PROFILE’])||isset($_SERVER[‘HTTP_PROFILE’])):
//echo ‘WAP BROWSER’;
break;

default:
// WEB BROWSER
break;
}

Advertisements