大家可以嘗試一下用手機(jī)訪問(wèn)我公司官方網(wǎng)站(www.xatjqxtz.com),系統(tǒng)會(huì)自動(dòng)識(shí)別并跳轉(zhuǎn)到手機(jī)版網(wǎng)站去;同樣的,如果使用電腦訪問(wèn)手機(jī)版網(wǎng)站(m.chuangluo.com),系統(tǒng)也會(huì)自動(dòng)識(shí)別并跳轉(zhuǎn)到對(duì)應(yīng)的電腦版網(wǎng)址去。目前已經(jīng)有很多同時(shí)擁有手機(jī)版網(wǎng)站和電腦版網(wǎng)站的大型企業(yè)網(wǎng)站使用了這樣的技術(shù),那小型公司網(wǎng)站如果也有這種需求的話,應(yīng)該如何實(shí)現(xiàn)呢?
實(shí)現(xiàn)手機(jī)版網(wǎng)站和電腦版網(wǎng)站的自動(dòng)識(shí)別并跳轉(zhuǎn)的條件
首先肯定是需要有兩個(gè)不同版本的網(wǎng)站,這是必然的!第二個(gè)條件是這兩個(gè)版本的網(wǎng)站數(shù)據(jù)要基本同步。以我公司官方網(wǎng)站為例,電腦版網(wǎng)站的頁(yè)面比手機(jī)版的多,手機(jī)版有的頁(yè)面,電腦版全部都有。這是因?yàn)槲覀冸娔X版網(wǎng)站上有很多靜態(tài)的專題頁(yè),而手機(jī)版則沒(méi)有,例如:網(wǎng)站價(jià)格評(píng)估、域名查詢等。因此,我們只需要實(shí)現(xiàn)自動(dòng)識(shí)別這一部分頁(yè)面并跳轉(zhuǎn)即可。
如何實(shí)現(xiàn)自動(dòng)識(shí)別并跳轉(zhuǎn)
由于我公司目前開發(fā)網(wǎng)站的語(yǔ)言是主流的PHP,因此我們此處只討論P(yáng)HP的實(shí)現(xiàn)方法,其他語(yǔ)言請(qǐng)直接參考本方法稍作修改即可。核心原理是通過(guò)程序判斷$_SERVER[?HTTP_USER_AGENT?]中是否包含了手機(jī)訪問(wèn)的一些特征信息,例如屏幕尺寸,手機(jī)品牌或者其他特征信息。此處一提供兩個(gè)方法供大家參考:
方法一:
判斷函數(shù)
function is_mob() {
$user_agent = $_SERVER[?HTTP_USER_AGENT?];
$mob_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","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_mob = false;
foreach ($mob_agents as $device) {
if (stristr($user_agent, $device)) {
$is_mob = true;
break;
}
}
return $is_mob;
}
調(diào)用函數(shù)
if(is_mob())
{
//通過(guò)$_SERVER["REQUEST_URI"]取出具體頁(yè)面網(wǎng)址,修改域名后并跳轉(zhuǎn)。
}
方法二:
$ua = strtolower($_SERVER[?HTTP_USER_AGENT?]);
$uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|mobile)/i"
if(($ua == ?? || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER[?REQUEST_URI?]),?wap?))
{
//通過(guò)$_SERVER["REQUEST_URI"]取出具體頁(yè)面網(wǎng)址,修改域名后并跳轉(zhuǎn)。
}
兩種方法中推薦使用第一種,識(shí)別率相對(duì)較高;第二種效率更高,但識(shí)別率相對(duì)較低。兩種方法均整理自互聯(lián)網(wǎng),大家在使用的時(shí)候可以稍加修改,以實(shí)現(xiàn)更多功能。