php判斷終端是手機(jī)還是電腦訪問網(wǎng)站的思路及代碼
更新時(shí)間:2013年04月24日 09:04:41 作者:
解下來將寫個(gè)小程序使用php判斷終端是手機(jī)還是電腦訪問網(wǎng)站,感興趣的朋友可以參考下哈,希望對(duì)你學(xué)習(xí)php有所幫助
代碼一:
<?php
function check_wap() {
if (isset($_SERVER['HTTP_VIA'])) return true;
if (isset($_SERVER['HTTP_X_NOKIA_CONNECTION_MODE'])) return true;
if (isset($_SERVER['HTTP_X_UP_CALLING_LINE_ID'])) return true;
if (strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0) {
// Check whether the browser/gateway says it accepts WML.
$br = "WML";
} else {
$browser = isset($_SERVER['HTTP_USER_AGENT']) ? trim($_SERVER['HTTP_USER_AGENT']) : '';
if(empty($browser)) return true;
$mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
$mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');
$found_mobile=checkSubstrs($mobile_os_list,$browser) ||
checkSubstrs($mobile_token_list,$browser);
if($found_mobile)
$br ="WML";
else $br = "WWW";
}
if($br == "WML") {
return true;
} else {
return false;
}
}
function checkSubstrs($list,$str){
$flag = false;
for($i=0;$i<count($list);$i++){
if(strpos($str,$list[$i]) > 0){
$flag = true;
break;
}
}
return $flag;
}
if(check_wap()){
echo "wap";
}else{
echo "web";
}
?>
代碼二:
<?php
header("Content-type:text/html;charset=utf-8");
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","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;
}
if(is_mobile()){
echo "手機(jī)";
}else{
echo "電腦";
}
復(fù)制代碼 代碼如下:
<?php
function check_wap() {
if (isset($_SERVER['HTTP_VIA'])) return true;
if (isset($_SERVER['HTTP_X_NOKIA_CONNECTION_MODE'])) return true;
if (isset($_SERVER['HTTP_X_UP_CALLING_LINE_ID'])) return true;
if (strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0) {
// Check whether the browser/gateway says it accepts WML.
$br = "WML";
} else {
$browser = isset($_SERVER['HTTP_USER_AGENT']) ? trim($_SERVER['HTTP_USER_AGENT']) : '';
if(empty($browser)) return true;
$mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
$mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');
$found_mobile=checkSubstrs($mobile_os_list,$browser) ||
checkSubstrs($mobile_token_list,$browser);
if($found_mobile)
$br ="WML";
else $br = "WWW";
}
if($br == "WML") {
return true;
} else {
return false;
}
}
function checkSubstrs($list,$str){
$flag = false;
for($i=0;$i<count($list);$i++){
if(strpos($str,$list[$i]) > 0){
$flag = true;
break;
}
}
return $flag;
}
if(check_wap()){
echo "wap";
}else{
echo "web";
}
?>
代碼二:
復(fù)制代碼 代碼如下:
<?php
header("Content-type:text/html;charset=utf-8");
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","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;
}
if(is_mobile()){
echo "手機(jī)";
}else{
echo "電腦";
}
您可能感興趣的文章:
- 無法在發(fā)生錯(cuò)誤時(shí)創(chuàng)建會(huì)話,請(qǐng)檢查 PHP 或網(wǎng)站服務(wù)器日志,并正確配置 PHP 安裝(win+linux)
- PHP實(shí)現(xiàn)批量檢測(cè)網(wǎng)站是否能夠正常打開的方法
- php檢測(cè)用戶是否用手機(jī)(Mobile)訪問網(wǎng)站的類
- phpsir 開發(fā) 一個(gè)檢測(cè)百度關(guān)鍵字網(wǎng)站排名的python 程序
- PHP如何解決網(wǎng)站大流量與高并發(fā)的問題
- php網(wǎng)站判斷用戶是否是手機(jī)訪問的方法
- PHP使用CURL實(shí)現(xiàn)對(duì)帶有驗(yàn)證碼的網(wǎng)站進(jìn)行模擬登錄的方法
- PHP獲取網(wǎng)站域名和地址的代碼
- 禁止IP訪問網(wǎng)站的多種方法分享(linux,php,nginx,apache)
- php提取字符串中網(wǎng)站url地址的方法
- PHP檢查網(wǎng)站是否宕機(jī)的方法示例
相關(guān)文章
Apache實(shí)現(xiàn)Web Server負(fù)載均衡詳解(不考慮Session版)
本篇文章是對(duì)使用Apache實(shí)現(xiàn)Web Server負(fù)載均衡的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下(不考慮Session版)2013-07-07PHP7擴(kuò)展開發(fā)之基于函數(shù)方式使用lib庫(kù)的方法詳解
這篇文章主要介紹了PHP7擴(kuò)展開發(fā)之基于函數(shù)方式使用lib庫(kù)的方法,結(jié)合實(shí)例形式分析了PHP7中l(wèi)ib庫(kù)擴(kuò)展的封裝與調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2018-01-01php出現(xiàn)內(nèi)存位置訪問無效錯(cuò)誤問題解決方法
這篇文章主要介紹了php出現(xiàn)內(nèi)存位置訪問無效錯(cuò)誤問題解決方法,本文分析后得出的原因是sqlite擴(kuò)展導(dǎo)致,需要的朋友可以參考下2014-08-08PHP中的traits實(shí)現(xiàn)代碼復(fù)用使用實(shí)例
這篇文章主要介紹了PHP中的traits實(shí)現(xiàn)代碼復(fù)用使用實(shí)例,本文講解了Trait簡(jiǎn)單使用、優(yōu)先級(jí)問題、多個(gè)Trait沖突問題、as可用來修改方法訪問控制、Trait中使用Trait等內(nèi)容,需要的朋友可以參考下2015-05-05php include和require的區(qū)別深入解析
本篇文章是對(duì)php中include與require的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06