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 相关文章推荐
一个阿拉伯数字转中文数字的函数
Oct 09 PHP
最省空间的计数器
Oct 09 PHP
php 文章采集正则代码
Dec 28 PHP
php中使用Imagick实现图像直方图的实现代码
Aug 30 PHP
php实现的返回数据格式化类实例
Sep 22 PHP
PHP fastcgi模式上传大文件(大约有300多K)报错
Sep 28 PHP
PHP制作图形验证码代码分享
Oct 23 PHP
php实现XSS安全过滤的方法
Jul 29 PHP
PHP消息队列用法实例分析
Feb 12 PHP
PHP的Laravel框架中使用AdminLTE模板来编写网站后台界面
Mar 21 PHP
php socket通信简单实现
Nov 18 PHP
Laravel重定向,a链接跳转,控制器跳转示例
Oct 22 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
Win2000+Apache+MySql+PHP4+PERL安装使用小结
2006/10/09 PHP
php 将excel导入mysql
2009/11/09 PHP
php-fpm中max_children的配置
2019/03/15 PHP
wordpress自定义标签云与随机获取标签的方法详解
2019/03/22 PHP
许愿墙中用到的函数
2006/10/07 Javascript
javascript document.images实例
2008/05/27 Javascript
jQuery 使用手册(二)
2009/09/23 Javascript
IE6下opacity与JQuery的奇妙结合
2013/03/01 Javascript
jquery foreach使用示例
2013/09/12 Javascript
JS实现切换标签页效果实例代码
2013/11/01 Javascript
jQuery焦点图切换简易插件制作过程全纪录
2014/08/27 Javascript
jquery实现动画菜单的左右滚动、渐变及图形背景滚动等效果
2015/08/25 Javascript
iframe中使用jquery进行查找的方法【案例分析】
2016/06/17 Javascript
JS对象是否拥有某属性如何判断
2017/02/03 Javascript
JS开发中百度地图+城市联动实现实时触发查询地址功能
2017/04/13 Javascript
AngularJS 最常用的八种功能(基础知识)
2017/06/26 Javascript
NodeJS安装图文教程
2018/04/19 NodeJs
JS使用setInterval实现的简单计时器功能示例
2018/04/19 Javascript
js中this的指向问题归纳总结
2018/11/28 Javascript
微信小程序制作扭蛋机代码实例
2019/09/24 Javascript
微信小程序实现列表的横向滑动方式
2020/07/15 Javascript
python定时检查启动某个exe程序适合检测exe是否挂了
2013/01/21 Python
python为tornado添加recaptcha验证码功能
2014/02/26 Python
Python正则获取、过滤或者替换HTML标签的方法
2016/01/28 Python
简单了解python字符串前面加r,u的含义
2019/12/26 Python
Kickers鞋英国官网:男士、女士和儿童鞋
2021/03/08 全球购物
PHP面试题附答案
2015/11/28 面试题
vue实现倒计时功能
2021/03/24 Vue.js
体育教师工作总结的自我评价
2013/10/10 职场文书
专业幼师实习生自我鉴定范文
2013/12/08 职场文书
小松树教学反思
2014/02/11 职场文书
正风肃纪查摆剖析材料
2014/10/10 职场文书
好媳妇事迹材料
2014/12/24 职场文书
电力培训学习心得体会
2016/01/11 职场文书
六年级作文之家庭作文
2019/12/12 职场文书
Python matplotlib可视化之绘制韦恩图
2022/02/24 Python