PHP和JAVASCRIPT判断访客终端是电脑还是手机
当用户使用手机等移动终端访问网站时,我们可以通过程序检测用户终端类型,如果是手机用户,则引导用户访问适配手机屏幕的移动站点。本文将介绍分别使用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;
}
上述代码中函数is_mobile()来判断用户终端类型,将收集到的当今各种手机的HTTP_USER_AGENT归结到数组$mobile_agents中,并进行匹配。使用时只需调用函数is_mobile()。如以下代码表示,当匹配用户为手机访问时,页面跳转到网站手机版m.helloweba.com。
if (is_mobile()) {
header('Location:http://m.helloweba.com');
} else {
echo '请使用手机访问.';
}
Javascript版
您也可以直接在前端页面上加入一段Javascript脚本来判断用户的终端类型。Javascript也是通过获取浏览器的user-agent信息,然后匹配已有的user-agent信息库。
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|
WebOS|Symbian|Windows Phone|Phone)/i))) {
location.replace("http://m.helloweba.com")
}else{
document.write("请使用手机访问.");
}
以上代码还不是很完善,有兴趣的朋友欢迎补充。
当然,我们也可以通过响应式布局来匹配各种不同屏幕,这样可以节约开发成本,但是当客户对移动网站的功能需求,对于独立的移动站点最好是采用在网站入口处判断用户访问终端类型,一般是我们在主站首页就做判断,如果是手机访客则跳转到手机版页面,否则按正常PC方式访问页面。
PHP和JAVASCRIPT判断访客终端是电脑还是手机的更多相关文章
- PHP判断访客是否移动端浏览器访问
今天要给大家分享一段PHP代码,该代码的功能是用来判断访客是否移动端浏览器访问,该功能的实现思路是通过HTTP_X_WAP_PROFILE. HTTP_VIA.HTTP_USER_AGENT等信息来判 ...
- PHP判断访问终端,电脑或手机访问
函数代码: //判断电脑或手机访问 function is_mobile(){ $user_agent = $_SERVER['HTTP_USER_AGENT']; $mobile_agents = ...
- js判断访客来源网址和关键字
加入现在有A.B两个站,A为友站,B为我站. 希望判断使用从A站连来时,就把访问者拒绝在门外,只要用简单几行的javascript,就可抓到使用的来源,以及作出一些防范的措施. <script ...
- 通过js判断访客显示器屏幕分辨率并给出提示
<script> <!-- fw="1920";fh="1200"; var uw=window.screen.width; var uh=w ...
- 判断用户 是用的电脑还是手机 判断 是安卓还是IOS
$(function(){ var Terminal = { // 辨别移动终端类型 platform: function () { var u = navigator.userAgent, app ...
- JS获取访客IP+判断归属地+自动跳转
由于公司业务需要,需要对网站特定地区的访客进行不同跳转.比如,上海的用户跳转到“shanghai.url.cn”,南京的用户跳转到“nanjing.url.cn”.下面就是我的实现方法,分享出来,顺便 ...
- javascript判断访问终端,手机端自动跳转
在网页的顶部加入javascript判断代码: function checkserAgent(){ var userAgentInfo=navigator.userAgent; var userAge ...
- File 与 Log #3--动态加入控件,[图片版]访客计数器(用.txt档案来记录)
File 与 Log #3--动态加入控件,[图片版]访客计数器(用.txt档案来记录) 以前的两篇文章(收录在书本「上集」的第十七章) 请看「ASP.NET专题实务」,松岗出版 File 与 Log ...
- JS获取访客IP进行自动跳转
因业务需要进行地区判断跳转指定站点,下面是我个人实现的办法,分享给大家,仅供参考,切勿做非法用途 第一步,获取IP并判断归属地 直接使用搜狐的IP库查询接口 <script type=" ...
随机推荐
- ZZ的计算器
Problem Description ZZ自从上大学以来,脑容量就是以SB计算的,这个吃货竟然连算术运算也不会了,可是当今的计算机可是非常强大的,作为ACMer, 几个简单的算术又算得了什么呢?可是 ...
- MongoDB C driver API continues
开篇前 <1,mongoc_init() func> mongoc_init() Synopsis void mongoc_init (void); Description This fu ...
- CSS 定位 (Positioning)学习
最近被css的定位要搞疯了...下面我总结一下最近学习东西. 先介绍几个概念: 块框:div.h1 或 p 元素常常被称为块级元素.这意味着这些元素显示为一块内容,即“块框”. 行内框:与之相反,sp ...
- js添加、删除Cookie
//cookie function addCookie(objName, objValue, objHours) { //添加cookie var str = objName + "=&qu ...
- Android核心基础(五)
1.仿网易新闻客户端 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...
- The 5th tip of DB Query Analyzer
The 5th tip of DB Query Analyzer Ma Genfeng (Guangdong UnitollServices incorporated, G ...
- HTTP概念进阶
1.什么是回调? 在Java中,就是类A调用类B中的某个方法b,然后类B又在某个时候反过来调用类A中的某个方法a,对于A来说,这个a方法便叫做回调方法 pubilc interface CallBac ...
- UVA 1613 K-Graph Oddity
题意; 在一个n个点的无向连通图中,n是奇数,k是使得所有点的度数不超过k的最小奇数,询问一种染色方案,使得相邻点的颜色不同. 分析: k=所有点中最大的入度.如果最大入度是偶数,则k+1.每个点可选 ...
- C# Socket服务端和客户端互相send和receive
服务端 { ]; ; { ...
- Object-C 重载
方法重载要保证三个条件 1在同一个类中 2.方法参数类型相同 名称相同 3.方法的参数不同 请看下面的例子 @interface whgMyObject : NSObject -(void)print ...