When users use mobile devices such as mobile phones to access the site, we can detect the user terminal type through the program, if it is a mobile phone user, then guide the user to the mobile site adapted to the mobile screen. This article will show you how to use PHP and JavaScript code to determine the user terminal type.
PHP version
We use PHP's $_server[' http_user_agent ' to get the user agent of the mobile phone user's browser, and then match the existing various mobile browser proxy libraries, if it contains matching keywords, it is judged as mobile (mobile terminal) users.
functionIs_mobile () {$user _agent=$_server[' Http_user_agent ']; $mobile _agents=Array("240x320", "Acer", "Acoon", "acs-", "Abacho", "Ahong", "Airness", "Alcatel", "Amoi", "Android", "anywhereyougo.com", " applewebkit/525 "," applewebkit/532 "," Asus "," Audio "," Au-mic "," Avantogo "," Becker "," BenQ "," Bilbo "," Bird "," BlackBerry "," blazer "," Bleu "," cdm-"," Compal "," Coolpad "," Danger "," Dbtel "," Dopod "," Elaine "," Eric "," Etouch "," Fly ", "Fly_", "fly-", "Go.web", "goodaccess", "Gradiente", "Grundig", "Haier", "Hedy", "Hitachi", "HTC", "Huawei", "Hutchison", "Inno", "ipad", "iPAQ", "iphone", "ipod", "Jbrowser", "KDDI", "kgt", "KWC", "Lenovo", "LG", "Lg2", "Lg3", "Lg4", "Lg5", "Lg7" , "Lg8", "Lg9", "lg-", "lge-", "Lge9", "Longcos", "Maemo", "Mercator", "Meridian", "Micromax", "MIDP", "Mini", "Mitsu", "mmm "MMP", "Mobi", "mot-", "Moto", "nec-", "NetFront", "Newgen", "Nexian", "Nf-browser", "Nintendo", "Nitro", "Nokia", "Nook "," Novarra "," Obigo "," Palm "," Panasonic "," Pantech "," Philips "," Phone "," pg-"," PlayStation "," Pocket "," pt-"," qc-"," Qtek "," Rover "," Sagem "," sama "," Samu "," Sanyo "," Samsung "," sch-"," Scooter "," seC "," Sendo "," sgh-"," Sharp "," Siemens "," sie-"," SoftBank "," Sony "," Spice "," Sprint "," SPV "," Symbian "," tablet "," Talkabout "," tcl-"," Teleca "," Telit "," Tianyu "," tim-"," Toshiba "," TSM "," Up.browser "," Utec "," Utstar "," Verykool "," Virgin "," vk-"," Voda "," Voxtel "," VX "," WAP "," Wellco "," Wig Browser "," Wii "," Windows CE "," wireless "," XDA "," Xde "," ZTE "); $is _mobile=false; foreach($mobile _agents as $device) { if(Stristr($user _agent,$device)) { $is _mobile=true; Break; } } return $is _mobile; }
JavaScript Edition
You can also include a JavaScript script directly on the front page to determine the user's terminal type. JavaScript also obtains user-agent information from the browser and then matches the existing User-agent repository.
if ((Navigator.userAgent.match (/(iphone|ipod| android|ios|ios|ipad| backerry| webos| symbian| Windows phone| Phone)) { location.replace ("http://m.helloweba.com")}else{ document.write ("Please use your phone to access.")
PHP and JS to determine the phone or computer access