php判断终端是手机还是电脑访问网站的思路及代码
2015-01-24信息快讯网
解下来将写个小程序使用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函数之日期时间函数date()使用详解
PHP json_encode中文乱码问题的解决办法
分享PHP header函数使用教程
php增删改查示例自己写的demo
php使用异或实现的加密解密实例
对淘宝URL中ID提取的PHP代码
显示程序执行时间php函数代码
测试PHP连接MYSQL成功与否的代码
PHP 通过Socket收发十六进制数据的实现代码
PHP计算2点经纬度之间的距离代码
PHP中::、->、self、$this几种操作符的区别介绍
用PHP提取中英文词语以及数字的首字母的方法介绍
基于PHP读取TXT文件向数据库导入海量数据的方法
『PHP』PHP截断函数mb_substr()使用介绍
怎样给PHP源代码加密?PHP二进制加密与解密的解决办法
基于PHP Web开发MVC框架的Smarty使用说明
php多文件上传功能实现原理及代码
ajax完美实现两个网页 分页功能的实例代码
做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)
简单的php数据库操作类代码(增,删,改,查)
php实现从ftp服务器上下载文件树到本地电脑的程序