php判断终端是手机还是电脑访问网站的思路及代码

 更新时间:2013年04月24日 09:04:41   作者:   我要评论
解下来将写个小程序使用php判断终端是手机还是电脑访问网站,感兴趣的朋友可以参考下哈,希望对你学习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 "手机";
}else{
echo "电脑";
}

相关文章

  • PHP切割汉字的常用方法实例总结

    PHP切割汉字的常用方法实例总结

    这篇文章主要介绍了PHP切割汉字的常用方法,结合实例形式总结分析了php针对汉字的编码转换、遍历、截取等相关操作技巧,需要的朋友可以参考下
    2019-04-04
  • php简单防盗链验证实现方法

    php简单防盗链验证实现方法

    这篇文章主要介绍了php简单防盗链验证实现方法,通过$_SERVER['HTTP_REFERER']获取来路页面URL再进行判断进而实现对非本地URL的拦截功能,需要的朋友可以参考下
    2016-07-07
  • linux下为php添加curl扩展的方法

    linux下为php添加curl扩展的方法

    一般php源码的扩展库里已经带了curl的扩展,所以没有必要下载,直接将编译扩展即可
    2011-07-07
  • 利用PHP判断是手机移动端还是PC端访问的函数示例

    利用PHP判断是手机移动端还是PC端访问的函数示例

    在平常工作开发中,我们通常需要开发出PC端和移动端两个不同的系统,从而根据访问端的不同进入到不同的操作界面中。下面这篇文章主要给大家介绍了关于利用PHP判断是手机移动端还是PC端访问的函数的相关资料,需要的朋友可以参考下。
    2017-12-12
  • 功能强大的PHP POST提交数据类

    功能强大的PHP POST提交数据类

    这篇文章主要为大家详细介绍了功能强大的PHP POST提交数据类,代码简洁且具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • PHP实现的只保留字符串首尾字符功能示例【隐藏部分字符串】

    PHP实现的只保留字符串首尾字符功能示例【隐藏部分字符串】

    这篇文章主要介绍了PHP实现的只保留字符串首尾字符功能,结合实例形式分析了php隐藏部分字符串相关的字符串遍历、截取相关操作技巧,需要的朋友可以参考下
    2019-03-03
  • php实现的redis缓存类定义与使用方法示例

    php实现的redis缓存类定义与使用方法示例

    这篇文章主要介绍了php实现的redis缓存类,结合具体实例形式分析了php封装的针对redis缓存类定义与使用相关操作技巧,需要的朋友可以参考下
    2017-08-08
  • php组合排序简单实现方法

    php组合排序简单实现方法

    这篇文章主要介绍了php组合排序简单实现方法,涉及php数据结构与数学运算相关操作技巧,需要的朋友可以参考下
    2016-10-10
  • php遍历删除整个目录及文件的方法

    php遍历删除整个目录及文件的方法

    这篇文章主要介绍了php遍历删除整个目录及文件的方法,涉及php操作目录及文件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • php 随机生成10位字符代码

    php 随机生成10位字符代码

    php 随机生成10位字符,大家可以看下原理就可以实现自定义位数的随机字符串了。
    2009-03-03

最新评论