TP3.2.x判断手机端访问并设置默认访问模块的方法 - ThinkPHP框架
手机端访问时调用Wap手机模块,实现在手机端访问时展示出手机网站,无需跳转域名
首先我们在./Application/Common/Conf/ 目录下建立两个公共配置文件:config.php 和config_wap.php
config.php 中:
- return array(
- 'DEFAULT_MODULE'=>'Index',
- 'DEFAULT_CONTROLLER'=>'Index',
- 'DEFAULT_ACTION'=>'index',
- )
config_wap.php 中:
- return array(
- 'DEFAULT_MODULE'=>'Wap',
- 'DEFAULT_CONTROLLER'=>'Index',
- 'DEFAULT_ACTION'=>'index',
- )
然后在入口文件 index.php 中加入手机端判断方法(建议放到最下面):
- function is_mobile_request(){
- $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
- $mobile_browser = '0';
- if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT'])))
- $mobile_browser++;
- if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))
- $mobile_browser++;
- if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
- $mobile_browser++;
- if(isset($_SERVER['HTTP_PROFILE']))
- $mobile_browser++;
- $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
- $mobile_agents = array(
- 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
- 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
- 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
- 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
- 'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
- 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
- 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
- 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
- 'wapr','webc','winw','winw','xda','xda-'
- );
- if(in_array($mobile_ua, $mobile_agents))
- $mobile_browser++;
- if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)
- $mobile_browser++;
- // Pre-final check to reset everything if the user is on Windows
- if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)
- $mobile_browser=0;
- // But WP7 is also Windows, with a slightly different characteristic
- if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false)
- $mobile_browser++;
- if($mobile_browser>0)
- return true;
- else
- return false;
- }
然后,我们开始用关键的APP_STATUS来调用不同的模块:
在index.php文件中的21行后面也就是“定义应用目录”结束后,加入代码:
- if(is_mobile_request()){
- define('APP_STATUS','config_wap');
- }else{
- define('APP_STATUS','Index');
- }
大功告成。
参考文档:http://document.thinkphp.cn/manual_3_2.html#load_config
TP3.2.x判断手机端访问并设置默认访问模块的方法 - ThinkPHP框架的更多相关文章
- ThinkPHP3.2判断手机端访问并设置默认访问模块的方法
ThinkPHP3.2判断是否为手机端访问并跳转到另一个模块的方法 目录结构 公共模块Common,Home模块,Mobile模块 配置Application/Common/Conf/config.p ...
- js判断手机端操作系统(Andorid/IOS)
非常实用的js判断手机端操作系统(Andorid/IOS),并自动跳转相应下载界面 androidURL = "http://xxx/xxx.apk"; var browser = ...
- js判断手机端操作系统的两种方法
//判断手机端操作系统(Andorid/IOS),并自动跳转相应下载界面 androidURL ="http://xxx/xxx.apk"; var browser = { ver ...
- 原生js为页面添加爱心特效和判断手机端还是电脑端登录
<!-- 为页面添加爱心特效 --> <script type="text/javascript"> (function (window, document ...
- dedecms织梦手机端文章内容页图片不能自适应解决方法
dedecms织梦手机端文章内容页图片不能自适应解决方法: 方法一修改手机端文章页模板代码: 找到并打开手机端的文章内容页模板,将里面的{dede:field.body/}标签修改一下,改为如下的标签 ...
- JavaScript 判断手机端访问并跳转 redirect mobile
假如你的手机端网站在 /m 目录下 (function(a,b){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer| ...
- PHP判断用户是手机端?还是浏览器端访问?
function isMobile(){ $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ' ...
- js判断手机端
if (window.location.toString().indexOf('pref=padindex') != -1) { } else { if (/AppleWebKit.*Mobile/i ...
- PHP 判断手机端还是web端
function isMobile(){ // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) re ...
随机推荐
- JDK源码阅读--AbstractStringBuilder
abstract class AbstractStringBuilder implements Appendable, CharSequence /** * The value is used for ...
- OpenCASCADE圆与平面求交
OpenCASCADE圆与平面求交 eryar@163.com 在 解析几何求交之圆与二次曲面中分析了OpenCASCADE提供的类IntAna_IntConicQuad可以用来计算圆与二次曲面之间的 ...
- 转:Linux 文件IO理解
源地址http://blog.csdn.net/lonelyrains/article/details/6604851 linux文件IO操作有两套大类的操作方式:不带缓存的文件IO操作,带缓存的文件 ...
- AppScan的基础使用
AppScan是用于Web项目的安全测试工具,扫描网站所有url,自动测试是否存在各种类型的漏洞.AppScan安装在Windows环境上,版本越高,规则库越安全,扫描越全面. 1. 打开AppS ...
- vue 组件内引入外部在线js、css
参考:https://blog.csdn.net/u010881899/article/details/80895661 例:引入element-ui js: mounted() { const oS ...
- DOS常用命令详解
DOS常用命令详解 dir 列文件名 deltree 删除目录树 cls 清屏 cd 改变当前目录 copy 拷贝文件 diskcopy 复制磁盘 del 删除文件 format 格式化磁盘 edit ...
- python configparser模块详解
此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...
- Hbase实验:java创建和删除table
开启zookeeper.hadoop.hbase: 打开eclipse创一个java project,然后导入所需jar包: 写好java代码,运行create,然后去hbase shell里查看: ...
- Leetcode201. Bitwise AND of Numbers Range数字范围按位与
给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点). 示例 1: 输入: [5,7] 输出: 4 ...
- AtCoder - 3959
AtCoder - 3959https://vjudge.net/problem/1583855/origin求最长连续递增长度就行,答案是n-max(len) #include<iostrea ...