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之第七天
Oct 09 PHP
PHP的单引号和双引号 字符串效率
May 27 PHP
色色整理的PHP面试题集锦
Mar 08 PHP
浅析PHP安装扩展mcrypt以及相关依赖项(PHP安装PECL扩展的方法)
Jul 05 PHP
深入解析PHP 5.3.x 的strtotime() 时区设定 警告信息修复
Aug 05 PHP
常见php数据文件缓存类汇总
Dec 05 PHP
thinkphp3.0输出重复两次的解决方法
Dec 19 PHP
php实现上传图片文件代码
Jul 19 PHP
php实现word转html的方法
Jan 22 PHP
Zend Framework教程之请求对象的封装Zend_Controller_Request实例详解
Mar 07 PHP
laravel5.4利用163邮箱发送邮件的步骤详解
Sep 22 PHP
PHP 自动加载类原理与用法实例分析
Apr 14 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
我的论坛源代码(五)
2006/10/09 PHP
如何判断php数组的维度
2013/06/10 PHP
解析zend Framework如何自动加载类
2013/06/28 PHP
PHP date函数常用时间处理方法
2015/05/11 PHP
PHP实现的随机IP函数【国内IP段】
2016/07/20 PHP
更正确的asp冒泡排序
2007/05/24 Javascript
javascript multibox 全选
2009/03/22 Javascript
jQuery中unwrap()方法用法实例
2015/01/16 Javascript
angularjs创建弹出框实现拖动效果
2020/08/25 Javascript
js注入 黑客之路必备!
2016/09/14 Javascript
深入理解bootstrap框架之第二章整体架构
2016/10/09 Javascript
微信小程序 MINA文件结构
2016/10/17 Javascript
JS实现的数字格式化功能示例
2017/02/10 Javascript
js记录点击某个按钮的次数-刷新次数为初始状态的实例
2017/02/15 Javascript
详解node.js平台下Express的session与cookie模块包的配置
2017/04/26 Javascript
详解nodejs http请求相关总结
2019/03/31 NodeJs
Vue+ElementUI项目使用webpack输出MPA的方法
2019/08/27 Javascript
layui下拉框获取下拉值(select)的例子
2019/09/10 Javascript
Node.js API详解之 os模块用法实例分析
2020/05/06 Javascript
javascript实现打砖块小游戏(附完整源码)
2020/09/18 Javascript
JavaScript实现网页计算器功能
2020/10/29 Javascript
vue.js实现点击图标放大离开时缩小的代码
2021/01/27 Vue.js
Python 私有函数的实例详解
2017/09/11 Python
TensorFlow实现RNN循环神经网络
2018/02/28 Python
Python实现的微信红包提醒功能示例
2019/08/22 Python
python每5分钟从kafka中提取数据的例子
2019/12/23 Python
详解pycharm2020.1.1专业版安装指南(推荐)
2020/08/07 Python
全球领先的美容用品专卖店:Beauty Plus Salon
2018/09/04 全球购物
拉飞逸官网:Lafayette 148 New York
2020/07/15 全球购物
管理学院毕业生自荐信范文
2014/03/10 职场文书
2014两会学习心得:榜样精神伴我行
2014/03/17 职场文书
离婚财产分隔协议书
2014/10/23 职场文书
go结构体嵌套的切片数组操作
2021/04/28 Golang
python实现三次密码验证的示例
2021/04/29 Python
25张裸眼3D图片,带你重温童年的记忆,感受3D的魅力
2022/02/06 杂记
Nginx图片服务器配置之后图片访问404的问题解决
2022/03/21 Servers