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 radio 单选框获取与保持值的实现代码
May 15 PHP
PHP用SAX解析XML的实现代码与问题分析
Aug 22 PHP
php多文件上传下载示例分享
Feb 20 PHP
php连接oracle数据库及查询数据的方法
Dec 29 PHP
php获取字符串中各个字符出现次数的方法
Feb 23 PHP
PHP的Yii框架的常用日志操作总结
Dec 08 PHP
用PHP写的一个冒泡排序法的函数简单实例
May 26 PHP
Yii2框架BootStrap样式的深入理解
Nov 07 PHP
ZendFramework框架实现连接两个或多个数据库的方法
Dec 08 PHP
关于ThinkPhp 框架表单验证及ajax验证问题
Jul 19 PHP
laravel框架与其他框架的详细对比
Oct 23 PHP
PHP底层运行机制与工作原理详解
Jul 31 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
解决phpmyadmin中文乱码问题。。。
2007/01/18 PHP
使用php计算排列组合的方法
2013/11/13 PHP
PHP结合jQuery插件ajaxFileUpload实现异步上传文件实例
2020/08/17 PHP
Zend Framework动作助手(Zend_Controller_Action_Helper)用法详解
2016/03/05 PHP
分析 JavaScript 中令人困惑的变量赋值
2007/08/13 Javascript
jquery 列表双向选择器之改进版
2013/08/09 Javascript
jquery通过a标签删除table中的一行的代码
2013/12/02 Javascript
详解jquery中$.ajax方法提交表单
2014/11/03 Javascript
node.js中的console.error方法使用说明
2014/12/10 Javascript
JS显示下拉列表框内全部元素的方法
2015/03/31 Javascript
JavaScript事件详细讲解
2016/06/27 Javascript
使用BootStrap和Metroui设计的metro风格微网站或手机app界面
2016/10/21 Javascript
AngularJS实现与Java Web服务器交互操作示例【附demo源码下载】
2016/11/02 Javascript
预防网页挂马的方法总结
2016/11/03 Javascript
JS数组搜索之折半搜索实现方法分析
2017/03/27 Javascript
Javascript ES6中数据类型Symbol的使用详解
2017/05/02 Javascript
js中document.write和document.writeln的区别
2018/03/11 Javascript
简单的三步vuex入门
2018/05/20 Javascript
脚手架vue-cli工程webpack的基本用法详解
2018/09/29 Javascript
微信小程序出现wx.getLocation再次授权问题的解决方法分析
2019/01/16 Javascript
javascript中的闭包概念与用法实践分析
2019/07/26 Javascript
在vue中实现echarts随窗体变化
2020/07/27 Javascript
[03:07]2015国际邀请赛选手档案EHOME.rOtK 是什么让他落泪?
2015/07/31 DOTA
10款最好的Web开发的 Python 框架
2015/03/18 Python
python数据结构之链表详解
2017/09/12 Python
pandas apply 函数 实现多进程的示例讲解
2018/04/20 Python
ITK 实现多张图像转成单个nii.gz或mha文件案例
2020/07/01 Python
Python常用库Numpy进行矩阵运算详解
2020/07/21 Python
python mock测试的示例
2020/10/19 Python
HTML5制作3D爱心动画教程 献给女友浪漫的礼物
2014/11/05 HTML / CSS
英国家电直销:Appliances Direct
2016/09/22 全球购物
英国Lookfantastic中文网站:护肤品美妆美发购物(英国直邮)
2020/04/27 全球购物
教师节演讲稿
2014/05/06 职场文书
2016三严三实专题教育活动心得体会
2016/01/06 职场文书
《从现在开始》教学反思
2016/02/16 职场文书
2019年行政人事个人工作总结范本!
2019/07/19 职场文书