Home > Web Front-end > JS Tutorial > Highlights of Determining Visitor Terminal Type_Javascript Tips

Highlights of Determining Visitor Terminal Type_Javascript Tips

WBOY
Release: 2016-05-16 15:56:22
Original
1389 people have browsed it

When a user uses a mobile terminal such as a mobile phone to access the website, we can detect the user terminal type through the program. If it is a mobile phone user, we will guide the user to access a mobile site adapted to the mobile phone screen. This article will introduce the use of PHP and JAVASCRIPT codes to determine the user terminal type.

PHP version

We use PHP's $_SERVER['HTTP_USER_AGENT'] to obtain the user agent of the mobile user browser, and then match various existing mobile browser agent libraries. If it contains matching keywords, it is determined to be 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; 
} 
Copy after login

The function is_mobile() in the above code is used to determine the user terminal type, summarize the collected HTTP_USER_AGENT of various mobile phones today into the array $mobile_agents, and perform matching. When using it, just call the function is_mobile(). As shown in the following code, when the matching user accesses via mobile phone, the page jumps to the mobile version of the website m.jb51.net.

 
if (is_mobile()) { 
  header('Location:http://m.jb51.net'); 
} else { 
  echo '请使用手机访问.'; 
} 
Copy after login

Javascript version

You can also add a Javascript script directly to the front-end page to determine the user's terminal type. Javascript also obtains the user-agent information of the browser and then 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("请使用手机访问."); 
} 
Copy after login

The above code is not complete yet, and interested friends are welcome to add to it.

Of course, we can also use responsive layout to match various screens, which can save development costs. However, when customers have functional requirements for mobile websites, for independent mobile sites, it is best to use user identification at the website entrance. Generally, we judge the access terminal type on the homepage of the main website. If it is a mobile visitor, it will jump to the mobile version page. Otherwise, access the page in the normal PC way.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template