Judge the visitor terminal type collection _ javascript skills

Source: Internet
Author: User
This article describes how to use php and javascript to determine the visitor terminal type. it is very simple and practical. For more information, see. When a user uses a mobile terminal such as a mobile phone to access a website, we can use a program to detect the type of the user terminal. if the user is a mobile phone user, the user is guided to access the mobile site adapted to the mobile phone screen. This article describes how to use PHP and JAVASCRIPT code to determine the user terminal type.

PHP version

We use PHP's $ _ SERVER ['http _ USER_AGENT '] to obtain the user agent of the mobile browser, and then match the existing agent libraries of various mobile browsers. if there is a matching keyword, it is regarded as a mobile phone (mobile terminal) user.

 function is_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; } 

In the above code, the function is_mobile () is used to determine the type of the user terminal. it sums the HTTP_USER_AGENT of various mobile phones collected to the array $ mobile_agents and performs matching. You only need to call the is_mobile () function (). The following code jumps to m.jb51.net of the website Mobile Edition when a matched user accesses the website through a mobile phone.

If (is_mobile () {header ('Location: http://m.jb51.net ');} else {echo' please visit with your phone .';}

Javascript version

You can also add a Javascript script on the front-end page to determine the terminal type of the user. Javascript also obtains the browser's user-agent information and matches the existing user-agent Information Library.

If (navigator. userAgent. match (/(iPhone | iPod | Android | ios | iOS | iPad | Backerry | WebOS | Symbian | Windows Phone | Phone)/I) {location. replace ("http://m.jb51.net")} else {document. write ("Please use your mobile phone to access. ");}

The above code is not perfect. if you are interested, please add it.

Of course, we can also use responsive la s to match different screens, which can save development costs. However, when the customer needs the features of a mobile website, for an independent mobile site, it is best to judge the type of the user's access terminal at the entrance of the website. generally, we will make a judgment on the homepage of the main site. if it is a mobile phone visitor, it will jump to the mobile page, otherwise, access the page in normal PC mode.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.