Identify visitor terminal Types Collection _javascript Tips

Source: Internet
Author: User

When users use mobile terminals such as mobile phones to visit the site, we can detect the user terminal type, if it is a mobile phone users, then guide users to access the mobile screen to adapt to cell phone site. This article describes using PHP and JavaScript code to determine the user terminal type separately.

PHP version

We use PHP's $_server[' http_user_agent '] to get the user agent of the mobile user's browser, then match the existing mobile browser agent library, if it contains a matching keyword, then judge the mobile (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", " "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; 
 }

The above code functions Is_mobile () to determine the user terminal type, the collection of today's various mobile phone http_user_agent to the array $mobile_agents, and matching. Only Call function Is_mobile () when used. As the following code indicates, when the matching user for the mobile phone access, the page jumps to the website mobile phone version m.jb51.net.

 
if (Is_mobile ()) { 
  header (' location:http://m.jb51.net '); 
} else { 
  echo ' please use mobile Access. ' 
} 

JavaScript version

You can also add a JavaScript script directly to the front-end page to determine the user's terminal type. JavaScript is also used to get the user-agent information of the browser and then match the existing user-agent repository.

 
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 mobile access."); 
} 

The above code is not very perfect, interested friends welcome to Add.

Of course, we can also use the response layout to match a variety of different screens, which can save the development costs, but when the customer's functional demand for mobile sites, for the independent mobile site is the best to use in the site to determine the user access terminal type, generally we are in the main home to do judgment, If it is a mobile phone visitors will jump to the mobile version of the page, otherwise the normal PC way to access the page.

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.