PHP和JS判断手机还是电脑访问
当用户使用手机等移动终端访问网站时,我们可以通过程序检测用户终端类型,如果是手机用户,则引导用户访问适配手机屏幕的移动站点。本文将介绍分别使用PHP和JAVASCRIPT代码判断用户终端类型。
PHP版
我们使用PHP的$_SERVER['HTTP_USER_AGENT']来获取手机用户浏览器的用户代理,然后匹配已有的各种手机浏览器代理库,如果含有匹配的关键字,则判断为手机(移动终端)用户。
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","iphone","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)) {
$is_mobile = true;
break;
}
}
return $is_mobile;
}
Javascript版
您也可以直接在前端页面上加入一段Javascript脚本来判断用户的终端类型。Javascript也是通过获取浏览器的user-agent信息,然后匹配已有的user-agent信息库。
if (navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Blackerry|WebOS|Symbian|Windows Phone|Phone|Nokia|UCWEB)/i)) {
alert('手机');
}else{
alert('PC');
}
PHP和JS判断手机还是电脑访问的更多相关文章
- JS判断手机还是电脑访问网站
function check() { let browser = navigator.userAgent;//用户访问的类型 let phone = ["Android", &qu ...
- PHP判断手机、电脑访问
/*判断用户是手机访问还是电脑访问*/$useragent = $_SERVER['HTTP_USER_AGENT']; if (preg_match('/(android|bb\d+|meego). ...
- java判断手机还是电脑访问
很多博客写的很复杂,没具体看.我只用下面的几行代码 String userAgent = req.getHeader("user-agent"); ) { //安卓 } ) || ...
- js判断是手机还是电脑访问网站
js判断是手机还是电脑访问网站 <script type="text/javascript"> <!- ...
- js判断手机 横屏模式
js判断手机 横屏模式 方法名称:orientation 实例: if(window.orientation!=0){ var obj=document.getElementById('orienta ...
- js判断手机浏览器操作系统和微信浏览器的方法
做手机端的前端开发,少不了对手机平台的判断.如,对于app下载,就要判断在Android平台下就显示Android下载提示:在iOS平台下就显示iOS下载提示. 今天就为大家介绍一下用js判断手机客户 ...
- JS判断手机访问页面,根据手机访问或者PC访问跳转
当用户访问你网站时,如果是PC端访问,则不作处理,如果是手机或者平板访问,就跳转到自己定义的手机页面去,这个在做webapp的时候经常用到,把代码分享给大家,希望对大家有所帮助. 首先,你要在页面中引 ...
- js判断手机的横竖屏调整样式
在移动端,我们经常遇到横竖屏的问题,所以我们改如何判断或针对横竖屏来写代码呢.首先需要在head中加入如下代码: <meta name="viewport" content= ...
- js判断手机浏览器是横屏or竖屏
移动端的浏览器一般都支持window.orientation这个参数,通过这个参数可以判断出手机是处在横屏还是竖屏状态. 从而根据实际需求而执行相应的程序.通过添加监听事件onorientationc ...
随机推荐
- Android常用异步任务执行方法
Handler原理及基本概念 Message 意为消息,发送到Handler进行处理的对象,携带描述信息和任意数据. MessageQueue 意为消息队列,Message的集合. Looper 有着 ...
- 坑爹的Adprep32.exe
今天看书的时候,看到用ADSI设置Universal Group Membership Caching,就想到用.Net Framework来实现,找了半天,才找到System.directoryse ...
- STL源码剖析(deque)
deque是一个双向开口的容器,在头尾两端进行元素的插入跟删除操作都有理想的时间复杂度. deque使用的是分段连续线性空间,它维护一个指针数组(T** map),其中每个指针指向一块连续线性空间. ...
- T-sql isnull函数介绍
今天在给同事调取数据的时候,同事反馈说数据偏少,我仔细检查,发现sql语句条件都正确,逻辑没哪里不对,最后经过仔细排查,才发现问题出在null字段上 表中有一列是允许为null值,比如查询名字不为测试 ...
- xml布局内容总结(三)--Android
关于xml中经经常使用到边框及边框效果,在此进行一下总结. 3.border(边框及边框效果) (1)直角边框线 <LinearLayout android:layout_wid ...
- Python 统计代码的行数,Python脚本 统计代码
# coding=utf-8 import os import time # 需要统计的文件夹或者文件,这是在windows下运行的,如果使用Linux系统可以使用 basedir = '/app/l ...
- vim跳出括号的方法
https://github.com/Raimondi/delimitMate delimitMate是一个自动括号补全的好插件,但是,如果没有一个好的跳出括号办法,好想由打了折扣. 我目前找到最适合 ...
- atitit.提升研发效率的利器---重型框架与类库的区别与设计原则
atitit.提升研发效率的利器---重型框架与类库的区别与设计原则 1. 框架的意义---设计的复用 1 1.1. 重型框架就是it界的重武器. 1 2. 框架 VS. 库 可视化图形化 1 2.1 ...
- ZOJ 3703 Happy Programming Contest(0-1背包)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3703 Happy Programming Contest Time Lim ...
- 谈一谈APP支付失败的处理
如题今天要描述一个问题是:程序在确认订单时拉起第三方支付,支付失败了,引起的问题. 为了能清楚的描述问题,我把场景复现一下,大家肯定都有过APP购物的体会,大家一定知道有一个按钮叫“确认”或者“结算” ...