thinkphp如何来判断访客为手机端或是PC端
发布:smiling 来源: PHP粉丝网 添加日期:2014-04-11 09:49:29 浏览: 评论:0
近日准备给自己的网站做一个小升级,让用户在手机二维码扫描的时候显示适合手机端来展示的模版【我用的是ThinkPHP3.0】,代码是参考别人的
我先说下大概的一个思路 简单两步:
统版本号和浏览器以及它的版本号(贴出的代码中数组中只有移动端信息,因此后面只需判断是否为数组中的某一个值即可).
根据数组中的值来判断访客是否为手机、pad、之类的移动端,如果是的话就规定项目路径、名称为你的手机端模版如 define('APP_NAME','mobi');
define('APP_PATH','./mobi/');
下面代码的话我尽量加上注释:
- //判断是否属手机
- //代码看上去很多,其实就是数组里面显得多而乱,不要被表面现象所吓倒哦!
- 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)) { //stristr 查找访客端信息是否在上述数组中,不存在即为PC端。
- $is_mobile = true;
- break;
- }
- }
- return $is_mobile;
- }
- define('THINK_PATH','./CORE/');
- if(is_mobile()){ //跳转至wap分组
- echo '您是手机端访问的,已跳转到手机端';
- define('APP_NAME','mobi');
- define('APP_PATH','./mobi/');
- }else{
- echo '你是PC端访问的';
- define('APP_NAME','Home');
- define('APP_PATH','./Home/');
- }
- define('APP_DEBUG', false);
- require THINK_PATH.'core.php';
Tags: think php手机端 PC端
相关文章
- ·ThinkPHP中自定义错误页面和提示页面 (2013-11-15)
- ·ThinkPHP中Ajax返回(2013-11-15)
- ·ThinkPHP中处理表单中注意(2013-11-15)
- ·ThinkPHP中I(),U(),$this->post()等函数(2013-11-15)
- ·ThinkPHP中公共函数路径和配置项路径的映射(2013-11-15)
- ·ThinkPHP中公共配置文件和各自项目中的配置文件组合(2013-11-15)
- ·ThinkPHP在控制器里的javascript代码不能执行解决方法(2013-11-29)
- ·ThinkPHP3.0略缩图不能保存到子目录(2013-12-03)
- ·thinkphp的循环结构(2014-01-10)
- ·thinkphp特殊标签使用(2014-01-10)
- ·thinkphp模板输出汇总(2014-01-10)
- ·thinkphp模板的赋值与替换(2014-01-10)
- ·thinkphp连贯操作(2014-01-10)
- ·thinkphp区间查询、统计查询、SQL直接查询(2014-01-10)
- ·thinkphp的普通查询与表达式查询(2014-01-10)
- ·RBAC类在ThinkPHP中的四种使用方法(2014-01-10)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)