随着移动设备的普及,网站也会迎来越来越多移动设备的访问。用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面。这里就介绍一下,如何判断用户是否用手机访问。
自定义的函数如下:
11 |
if ( stristr ( $_SERVER [ 'HTTP_VIA' ], "wap" )){ |
15 |
elseif ( strpos ( strtoupper ( $_SERVER [ 'HTTP_ACCEPT' ]), "VND.WAP.WML" ) > 0){ |
19 |
elseif (preg_match( '/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i' , $_SERVER [ 'HTTP_USER_AGENT' ])){ |
再来一个从PHP框架剥离的判断是否为手机移动终端的函数:
01 |
function is_mobile_request() |
03 |
$_SERVER [ 'ALL_HTTP' ] = isset( $_SERVER [ 'ALL_HTTP' ]) ? $_SERVER [ 'ALL_HTTP' ] : '' ; |
04 |
$mobile_browser = '0' ; |
05 |
if (preg_match( '/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i' , strtolower ( $_SERVER [ 'HTTP_USER_AGENT' ]))) |
07 |
if ((isset( $_SERVER [ 'HTTP_ACCEPT' ])) and ( strpos ( strtolower ( $_SERVER [ 'HTTP_ACCEPT' ]), 'application/vnd.wap.xhtml+xml' ) !== false)) |
09 |
if (isset( $_SERVER [ 'HTTP_X_WAP_PROFILE' ])) |
11 |
if (isset( $_SERVER [ 'HTTP_PROFILE' ])) |
13 |
$mobile_ua = strtolower ( substr ( $_SERVER [ 'HTTP_USER_AGENT' ],0,4)); |
14 |
$mobile_agents = array ( |
15 |
'w3c ' , 'acs-' , 'alav' , 'alca' , 'amoi' , 'audi' , 'avan' , 'benq' , 'bird' , 'blac' , |
16 |
'blaz' , 'brew' , 'cell' , 'cldc' , 'cmd-' , 'dang' , 'doco' , 'eric' , 'hipt' , 'inno' , |
17 |
'ipaq' , 'java' , 'jigs' , 'kddi' , 'keji' , 'leno' , 'lg-c' , 'lg-d' , 'lg-g' , 'lge-' , |
18 |
'maui' , 'maxo' , 'midp' , 'mits' , 'mmef' , 'mobi' , 'mot-' , 'moto' , 'mwbp' , 'nec-' , |
19 |
'newt' , 'noki' , 'oper' , 'palm' , 'pana' , 'pant' , 'phil' , 'play' , 'port' , 'prox' , |
20 |
'qwap' , 'sage' , 'sams' , 'sany' , 'sch-' , 'sec-' , 'send' , 'seri' , 'sgh-' , 'shar' , |
21 |
'sie-' , 'siem' , 'smal' , 'smar' , 'sony' , 'sph-' , 'symb' , 't-mo' , 'teli' , 'tim-' , |
22 |
'tosh' , 'tsm-' , 'upg1' , 'upsi' , 'vk-v' , 'voda' , 'wap-' , 'wapa' , 'wapi' , 'wapp' , |
23 |
'wapr' , 'webc' , 'winw' , 'winw' , 'xda' , 'xda-' |
25 |
if (in_array( $mobile_ua , $mobile_agents )) |
27 |
if ( strpos ( strtolower ( $_SERVER [ 'ALL_HTTP' ]), 'operamini' ) !== false) |
29 |
// Pre-final check to reset everything if the user is on Windows |
30 |
if ( strpos ( strtolower ( $_SERVER [ 'HTTP_USER_AGENT' ]), 'windows' ) !== false) |
32 |
// But WP7 is also Windows, with a slightly different characteristic |
33 |
if ( strpos ( strtolower ( $_SERVER [ 'HTTP_USER_AGENT' ]), 'windows phone' ) !== false) |
最新文章
- C#对图片的操作
- Android学习
- [fortify] open redirect漏洞
- 实现解耦-Spring.Net
- UItextView回收键盘的几种方式
- 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性
- HDU 2676 Network Wars 01分数规划,最小割 难度:4
- What is XMLHTTP? How to use security zones in Internet Explorer
- iOS app闪退的一般原因
- tabBar隐藏与显现 hidesBottomBarWhenPushed
- append与after区别
- ajax对服务器路径请求
- mysql版sql助记
- Imagine Cup 微软“创新杯”全球学生科技大赛
- 计蒜客蓝桥杯模拟赛五J. 程序设计:放置守卫
- 【Salvation】——关卡功能&;数据库基础实现
- Centos7搭建FTP服务器
- Java值传递和引用传递
- python脚本练习之编译安装python
- python之https爬虫出现 SSL: CERTIFICATE_VERIFY_FAILED (同时打开fiddler就会出现)
热门文章
- Mvvm绑定datagrid或listview的selectItems的方法[转]
- apache-php安装mysql简单方法
- 由Python的super()函数想到的
- Python Tutorial 学习(五)--Data Structures
- 使用windows live writer 编辑博客日志
- ASP.NET MVC轻教程 Step By Step 2 ——View初探
- 原 IOS之NSValue整理
- Flex DataGrid 添加控件
- codeforces Unusual Product
- mysql数据库操作语句大全