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)文章
Windows環(huán)境下安裝PHP Pear的方法圖文教程
這篇文章主要介紹了Windows環(huán)境下安裝PHP Pear的方法,結(jié)合圖文形式詳細(xì)說明了Windows環(huán)境下安裝PHP Pear的相關(guān)命令與操作技巧,需要的朋友可以參考下2019-07-07
PHP封裝函數(shù)實(shí)現(xiàn)生成隨機(jī)的字符串驗(yàn)證碼
這篇文章主要介紹了利用PHP封裝函數(shù)實(shí)現(xiàn)生成隨機(jī)的字符串驗(yàn)證碼的相關(guān)資料,文中給出了詳細(xì)的介紹的示例代碼,對(duì)大家具有一定的參考價(jià)值,有需要的朋友們可以一起來學(xué)習(xí)學(xué)習(xí)。2017-01-01
探討如何使用SimpleXML函數(shù)來加載和解析XML文檔
本篇文章是對(duì)使用SimpleXML函數(shù)來加載和解析XML文檔進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php 廣告調(diào)用類代碼(支持Flash調(diào)用)
php 廣告調(diào)用類代碼(支持Flash調(diào)用),非常方便php頁面中加載廣告。需要的朋友可以參考下。2011-08-08
PHP通過內(nèi)置函數(shù)memory_get_usage()獲取內(nèi)存使用情況
這篇文章主要介紹了PHP通過內(nèi)置函數(shù)memory_get_usage()獲取內(nèi)存使用情況,需要的朋友可以參考下2014-11-11
解決nginx不支持thinkphp中pathinfo的問題
這篇文章主要介紹如何解決當(dāng)Nginx不支持pathinfo問題時(shí)該如何解決,需要的朋友可以參考下2015-07-07
PHP實(shí)現(xiàn)自動(dòng)識(shí)別Restful API的返回內(nèi)容類型
這篇文章主要介紹了PHP實(shí)現(xiàn)自動(dòng)識(shí)別Restful API的返回內(nèi)容類型,并實(shí)現(xiàn)自動(dòng)自動(dòng)渲染成 json、xml、html、serialize、csv、php等數(shù)據(jù)格式輸出,需要的朋友可以參考下2015-02-02
PHP直接修改表內(nèi)容DataGrid功能實(shí)現(xiàn)代碼
最近想做一個(gè)通過PHP實(shí)現(xiàn)DataGrid功能的東西,這樣可以直接修改數(shù)據(jù)庫中表的內(nèi)容,而不用開發(fā)【新增數(shù)據(jù)頁面】和【編輯頁面】,本篇首先介紹基于MySQL的使用方法,再簡(jiǎn)單介紹對(duì)于Oracle連接(基于sqlrelay)的二次開發(fā)。2015-09-09
深入解析fsockopen與pfsockopen的區(qū)別
本篇文章是對(duì)fsockopen與pfsockopen的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07

