When users use mobile phones and other mobile devices to access the site, we can detect a user terminal type program, if it is a mobile phone user, you guide the user to access the mobile phone screen adaptation sites. This article describes the use of PHP and javascript code separately to determine the user terminal type.
PHP version
We use PHP's $ _SERVER ['HTTP_USER_AGENT'] to get the mobile phone user's browser user agent, then match various existing mobile browser proxy library, if it contains the keyword matching, it is determined that a mobile phone (mobile terminal) users .
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;
}
The code in the function is_mobile () to determine the type of user terminals, will be collected from a variety of mobile phones today HTTP_USER_AGENT attributed to the array $ mobile_agents in and match. Just call the function is_mobile () when used. As the following code shows that when matching users mobile access, the page jumps to Mobile version m.goocode.net.
if (is_mobile()) {
header('Location:http://m.goocode.net');
} else {
echo 'Please use mobile to browse.';
}
javascript version
You can also add some javascript script directly on the front page to determine the user's terminal type. javascript is by getting the browser user-agent information, and then match the existing user-agent information database.
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|
WebOS|Symbian|Windows Phone|Phone)/i))) {
location.replace("http://m.goocode.net")
}else{
document.write("Please use mobile to browse.");
}
The above code is not perfect, welcome interested friends for deep supplement.
Of course, we can also respond to a variety of different layout to match the screen, so you can save development costs, but when the customer functional requirements for mobile sites, mobile sites for independence is preferable to use the entrance at the site to determine the type of user access terminal generally we do in the master page judgment, if it is a mobile phone visitors jump to mobile version page, otherwise normal PC mode to access the page.