typecho获取用户UA实现方法
首先我们要弄清楚UA是啥,不要想要这个东西,但是不知道他是啥,那就尴尬了。
User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。
将如下代码添加至 function.php 文件中
- // 获取浏览器信息
- function getBrowser($agent)
- {
- if (preg_match('/MSIE\s([^\s|;]+)/i', $agent, $regs)) {
- $outputer = 'IE浏览器';
- } else if (preg_match('/FireFox\/([^\s]+)/i', $agent, $regs)) {
- $str1 = explode('Firefox/', $regs[0]);
- $FireFox_vern = explode('.', $str1[1]);
- $outputer = '火狐浏览器 '. $FireFox_vern[0];
- } else if (preg_match('/Maxthon([\d]*)\/([^\s]+)/i', $agent, $regs)) {
- $str1 = explode('Maxthon/', $agent);
- $Maxthon_vern = explode('.', $str1[1]);
- $outputer = '傲游浏览器 '.$Maxthon_vern[0];
- } else if (preg_match('#SE 2([a-zA-Z0-9.]+)#i', $agent, $regs)) {
- $outputer = '搜狗浏览器';
- } else if (preg_match('#360([a-zA-Z0-9.]+)#i', $agent, $regs)) {
- $outputer = '360浏览器';
- } else if (preg_match('/Edge([\d]*)\/([^\s]+)/i', $agent, $regs)) {
- $str1 = explode('Edge/', $regs[0]);
- $Edge_vern = explode('.', $str1[1]);
- $outputer = 'Edge '.$Edge_vern[0];
- } else if (preg_match('/UC/i', $agent)) {
- $str1 = explode('rowser/', $agent);
- $UCBrowser_vern = explode('.', $str1[1]);
- $outputer = 'UC浏览器 '.$UCBrowser_vern[0];
- } else if (preg_match('/MicroMesseng/i', $agent, $regs)) {
- $outputer = '微信内嵌浏览器';
- } else if (preg_match('/WeiBo/i', $agent, $regs)) {
- $outputer = '微博内嵌浏览器';
- } else if (preg_match('/QQ/i', $agent, $regs)||preg_match('/QQBrowser\/([^\s]+)/i', $agent, $regs)) {
- $str1 = explode('rowser/', $agent);
- $QQ_vern = explode('.', $str1[1]);
- $outputer = 'QQ浏览器 '.$QQ_vern[0];
- } else if (preg_match('/BIDU/i', $agent, $regs)) {
- $outputer = '百度浏览器';
- } else if (preg_match('/LBBROWSER/i', $agent, $regs)) {
- $outputer = '猎豹浏览器';
- } else if (preg_match('/TheWorld/i', $agent, $regs)) {
- $outputer = '世界之窗浏览器';
- } else if (preg_match('/XiaoMi/i', $agent, $regs)) {
- $outputer = '小米浏览器';
- } else if (preg_match('/UBrowser/i', $agent, $regs)) {
- $str1 = explode('rowser/', $agent);
- $UCBrowser_vern = explode('.', $str1[1]);
- $outputer = 'UC浏览器 '.$UCBrowser_vern[0];
- } else if (preg_match('/mailapp/i', $agent, $regs)) {
- $outputer = 'email内嵌浏览器';
- } else if (preg_match('/2345Explorer/i', $agent, $regs)) {
- $outputer = '2345浏览器';
- } else if (preg_match('/Sleipnir/i', $agent, $regs)) {
- $outputer = '神马浏览器';
- } else if (preg_match('/YaBrowser/i', $agent, $regs)) {
- $outputer = 'Yandex浏览器';
- } else if (preg_match('/Opera[\s|\/]([^\s]+)/i', $agent, $regs)) {
- $outputer = 'Opera浏览器';
- } else if (preg_match('/MZBrowser/i', $agent, $regs)) {
- $outputer = '魅族浏览器';
- } else if (preg_match('/VivoBrowser/i', $agent, $regs)) {
- $outputer = 'vivo浏览器';
- } else if (preg_match('/Quark/i', $agent, $regs)) {
- $outputer = '夸克浏览器';
- } else if (preg_match('/mixia/i', $agent, $regs)) {
- $outputer = '米侠浏览器';
- }else if (preg_match('/fusion/i', $agent, $regs)) {
- $outputer = '客户端';
- } else if (preg_match('/CoolMarket/i', $agent, $regs)) {
- $outputer = '基安内置浏览器';
- } else if (preg_match('/Thunder/i', $agent, $regs)) {
- $outputer = '迅雷内置浏览器';
- } else if (preg_match('/Chrome([\d]*)\/([^\s]+)/i', $agent, $regs)) {
- $str1 = explode('Chrome/', $agent);
- $chrome_vern = explode('.', $str1[1]);
- $outputer = 'Chrome '.$chrome_vern[0];
- } else if (preg_match('/safari\/([^\s]+)/i', $agent, $regs)) {
- $str1 = explode('Version/', $agent);
- $safari_vern = explode('.', $str1[1]);
- $outputer = 'Safari '.$safari_vern[0];
- } else{
- $outputer = '?浏览器';
- }
- echo $outputer;
- }
- // 获取操作系统信息
- function getOs($agent)
- {
- $os = false;
-
- if (preg_match('/win/i', $agent)) {
- if (preg_match('/nt 6.0/i', $agent)) {
- $os = '<i class="fa fa-windows"></i> Vista';
- } else if (preg_match('/nt 6.1/i', $agent)) {
- $os = '<i class="fa fa-windows"></i> 7';
- } else if (preg_match('/nt 6.2/i', $agent)) {
- $os = '<i class="fa fa-windows"></i> 8';
- } else if(preg_match('/nt 6.3/i', $agent)) {
- $os = '<i class="fa fa-windows"></i> 8.1';
- } else if(preg_match('/nt 5.1/i', $agent)) {
- $os = '<i class="fa fa-windows"></i> XP';
- } else if (preg_match('/nt 10.0/i', $agent)) {
- $os = '<i class="fa fa-windows"></i> 10';
- } else{
- $os = '<i class="fa fa-windows"></i>';
- }
- } else if (preg_match('/android/i', $agent)) {
- if (preg_match('/android 9/i', $agent)) {
- $os = '<i class="fa fa-android"></i> P';
- }
- else if (preg_match('/android 8/i', $agent)) {
- $os = '<i class="fa fa-android"></i> O';
- }
- else if (preg_match('/android 7/i', $agent)) {
- $os = '<i class="fa fa-android"></i> N';
- }
- else if (preg_match('/android 6/i', $agent)) {
- $os = '<i class="fa fa-android"></i> M';
- }
- else if (preg_match('/android 5/i', $agent)) {
- $os = '<i class="fa fa-android"></i> L';
- }
- else{
- $os = '<i class="fa fa-android"></i>';
- }
- }
- else if (preg_match('/ubuntu/i', $agent)) {
- $os = '<i class="fa fa-linux"></i>';
- } else if (preg_match('/linux/i', $agent)) {
- $os = '<i class="fa fa-linux"></i>';
- } else if (preg_match('/iPhone/i', $agent)) {
- $os = '<i class="fa fa-apple"></i>';
- } else if (preg_match('/mac/i', $agent)) {
- $os = '<i class="fa fa-apple"></i>';
- }else if (preg_match('/fusion/i', $agent)) {
- $os = '<i class="fa fa-android"></i>';
- } else {
- $os = '?系统';
- }
- echo $os;
- }
-
调用方法
在 comments.php 合适的位置,按照如下方法调用即可。
- <!-- 获取浏览器信息 -->
- <?php getBrowser($comments->agent); ?>
- <!-- 获取操作系统信息 -->
- <?php getOs($comments->agent); ?>
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/61
0 评论
2.7k
发表评论
热门文章
自媒体博客Spimes主题44w 阅读
Spimes主题专为博客、自媒体、资讯类的网站设计....
Splity博客双栏主题14w 阅读
仿制主题,Typecho博客主题,昼夜双版设计,可....
vCard主题个人简历主题13w 阅读
一款个人简历主题,可以简单搭建一下,具体也比较简单....
Spzac个人资讯下载类主题12w 阅读
用于作品展示、资源下载,行业垂直性网站、个人博客,....
热评文章
自媒体博客Spimes主题423 评论
Splity博客双栏主题191 评论
Spzac个人资讯下载类主题89 评论
Splinx博客图片主题35 评论
Spzhi知识付费社区主题34 评论
三栏清新博客S_blog主题31 评论
vCard主题个人简历主题29 评论
Pure轻简主题29 评论
油腻男
2天前
感谢分享