thinkphp判断访客为手机端或PC端的方法


Posted in PHP onNovember 24, 2014

本文实例讲述了thinkphp判断访客为手机端或PC端的方法。分享给大家供大家参考。具体实现方法如下:

一、问题:

近日准备给自己的网站做一个小升级,让用户在手机二维码扫描的时候显示适合手机端来展示的模版【我用的是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';

希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

PHP 相关文章推荐
php后台程序与Javascript的两种交互方式
Oct 25 PHP
php下关于中英数字混排的字符串分割问题
Apr 06 PHP
php 删除记录同时删除图片文件的实现代码
May 12 PHP
php实现文件下载实例分享
Jun 02 PHP
浅谈php自定义错误日志
Feb 13 PHP
php实现插入数组但不影响原有顺序的方法
Mar 27 PHP
php实现读取内存顺序号
Mar 29 PHP
php截取视频指定帧为图片
May 16 PHP
微信支付的开发流程详解
Sep 13 PHP
PHP会话控制实例分析
Dec 24 PHP
Laravel中任务调度console使用方法小结
May 07 PHP
php实现往pdf中加数字签名操作示例【附源码下载】
Aug 07 PHP
ThinkPHP公共配置文件与各自项目中配置文件组合的方法
Nov 24 #PHP
php使用str_replace实现输入框回车替换br的方法
Nov 24 #PHP
thinkphp循环结构用法实例
Nov 24 #PHP
thinkphp特殊标签用法概述
Nov 24 #PHP
PHP中mysql_field_type()函数用法
Nov 24 #PHP
thinkphp模板输出技巧汇总
Nov 24 #PHP
php中mt_rand()随机数函数用法
Nov 24 #PHP
You might like
PHP实现中文圆形印章特效
2015/06/19 PHP
PHP实现与java 通信的插件使用教程
2019/08/11 PHP
avalonjs制作响应式瀑布流特效
2015/05/06 Javascript
jfinal与bootstrap的登录跳转实战演习
2015/09/22 Javascript
JavaScript中输出信息的方法(信息确认框-提示输入框-文档流输出)
2016/06/12 Javascript
BootStrap智能表单实战系列(十一)级联下拉的支持
2016/06/13 Javascript
Javascript打印局部页面实例
2016/06/21 Javascript
jQuery中fadein与fadeout方法用法示例
2016/09/16 Javascript
在点击div中的p时,如何阻止事件冒泡
2017/02/07 Javascript
微信小程序 支付功能(前端)的实现
2017/05/24 Javascript
JavaScript数组特性与实践应用深入详解
2018/12/30 Javascript
微信小程序自定义组件实现环形进度条
2020/11/17 Javascript
Vue实现点击按钮复制文本内容的例子
2019/11/09 Javascript
高效jQuery选择器的5个技巧实例分析
2019/11/26 jQuery
webpack 如何同时输出压缩和未压缩的文件的实现步骤
2020/06/05 Javascript
python实现从ftp服务器下载文件的方法
2015/04/30 Python
Python的math模块中的常用数学函数整理
2016/02/04 Python
详解Python中的array数组模块相关使用
2016/07/05 Python
浅谈Python NLP入门教程
2017/12/25 Python
Python实现的视频播放器功能完整示例
2018/02/01 Python
python读取xlsx的方法
2018/12/25 Python
基于Python编写一个计算器程序,实现简单的加减乘除和取余二元运算
2020/08/05 Python
Python中对象的比较操作==和is区别详析
2021/02/12 Python
数以千计的折扣工业产品:ESE Direct
2018/05/20 全球购物
台湾演唱会订票网站:StubHub台湾
2019/06/11 全球购物
集世界奢侈品和设计师品牌的意大利精品买手店:Tessabit
2019/08/17 全球购物
毕业生个人的自我评价优秀范文
2013/10/03 职场文书
中专生毕业自我鉴定
2013/11/01 职场文书
幼儿园保育员辞职信
2014/01/12 职场文书
市政施工员自我鉴定
2014/01/15 职场文书
店长职务说明书
2014/02/04 职场文书
优秀护士获奖感言
2014/02/20 职场文书
租房协议书怎么写
2014/04/10 职场文书
2015应届毕业生求职信范文
2015/03/20 职场文书
2015大学迎新晚会主持词
2015/07/16 职场文书
公开致歉信
2019/06/24 职场文书