php ua解析,UA识别有什么用?

ua识别的作用是可以使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件,从而判断用户是使用电脑浏览还是手机浏览,让网页作出自动的适应。

65ba552a246181a14e2a3ef777574b6b.png

UA识别有什么用?

ua识别(浏览器标识识别)可以使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件,从而判断用户是使用电脑浏览还是手机浏览,让网页作出自动的适应。

可理解为网站通过对ua标示的判别,可按相应的格式进行网页的布局调整,使用户获得更好的浏览体验。

设置成哪个(Android、iphone、ipad和电脑)就相当于用哪种设备浏览网页。

以qq手机浏览器为例,介绍查看更改ua标示的方法:

1、打开“手机QQ浏览器”,在底部栏中点击中间的按钮,展开菜单列表,选择“设置”。进入设置界面后,就能看到“浏览器UA标识”的设置。

bfdba95a0ec8da575b13248f5f69ae1b.png

2、打开“浏览器UA标识”,用户就能看到不同的UA标识,有Android、iphone、ipad和电脑。如何想把浏览效果变成电脑的话,那么就选择“电脑”。选择完后,输入一个网站查看,原本的手机浏览效果就变回电脑版的浏览效果了。

b0061444ac8c8e7eb3191363866d9560.png

如果设置成无的话就相当于用电脑浏览器上网,设置成iphone的话就是用iphone上网一样,只是浏览器还是手机的浏览器罢了

精准快速的UA识别PHP代码function user_agent($ua) {

//开始解析操作系统

$os = null;

if (preg_match('/Windows NT 6.0/i', $ua)) {

$os = "Windows Vista";

} elseif (preg_match('/Windows NT 6.1/i', $ua)) {

$os = "Windows 7";

} elseif (preg_match('/Windows NT 6.2/i', $ua)) {

$os = "Windows 8";

} elseif (preg_match('/Windows NT 6.3/i', $ua)) {

$os = "Windows 8.1";

} elseif (preg_match('/Windows NT 10.0/i', $ua)) {

$os = "Windows 10";

} elseif (preg_match('/Windows NT 5.1/i', $ua)) {

$os = "Windows XP";

} elseif (preg_match('/Mac OS X/i', $ua)) {

$os = "Mac OS X";

} elseif (preg_match('#Linux#i', $ua)) {

$os = "Linux ";

} elseif (preg_match('#Windows Phone#i', $ua)) {

$os = "Windows Phone ";

} elseif (preg_match('/Windows NT 5.2/i', $ua) && preg_match('/Win64/i', $ua)) {

$os = "Windows XP 64 bit";

} elseif (preg_match('/Android ([0-9.]+)/i', $ua, $matches)) {

$os = "Android " . $matches[1];

} elseif (preg_match('/iPhone OS ([_0-9]+)/i', $ua, $matches)) {

$os = 'iPhone ' . $matches[1];

} else {

$os = '未知操作系统';

}

if (preg_match('#(Camino|Chimera)[ /]([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = 'Camino ' . $matches[2];

} elseif (preg_match('#SE 2([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = '搜狗浏览器 2' . $matches[1];

} elseif (preg_match('#360([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = '360浏览器 ' . $matches[1];

} elseif (preg_match('#Maxthon( |\/)([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = 'Maxthon 浏览器' . $matches[2];

} elseif (preg_match('#Chrome/([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = 'Chrome ' . $matches[1];

} elseif (preg_match('#XiaoMi/MiuiBrowser/([0-9.]+)#i', $ua, $matches)) {

$browser = '小米浏览器 ' . $matches[1];

} elseif (preg_match('#Safari/([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = 'Safari浏览器 ' . $matches[1];

} elseif (preg_match('#opera mini#i', $ua)) {

preg_match('#Opera/([a-zA-Z0-9.]+)#i', $ua, $matches);

$browser = 'Opera Mini ' . $matches[1];

} elseif (preg_match('#Opera.([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = 'Opera ' . $matches[1];

} elseif (preg_match('#TencentTraveler ([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = '腾讯TT浏览器 ' . $matches[1];

} elseif (preg_match('#(UCWEB|UBrowser|UCBrowser)/([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = 'UC浏览器 ' . $matches[1];

} elseif (preg_match('#Vivaldi/([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = 'Vivaldi浏览器 ' . $matches[1];

} elseif (preg_match('#wp-(iphone|android)/([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = 'WordPress客户端 ' . $matches[1];

} elseif (preg_match('#Edge ([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = '微软Edge浏览器 ' . $matches[1];

} elseif (preg_match('#MSIE ([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = 'Internet Explorer ' . $matches[1];

} elseif (preg_match('#(Firefox|Phoenix|SeaMonkey|Firebird|BonEcho|GranParadiso|Minefield|Iceweasel)/([a-zA-Z0-9.]+)#i', $ua, $matches)) {

$browser = 'Firefox浏览器 ' . $matches[2];

} else {

$browser = '未知浏览器';

}

return $os . " | " . $browser;

}

更多相关知识,请访问 PHP中文网!!

相关标签:UA识别

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值