• 首页
  • 搜索
  • 夜间模式
    ©2012-2025  Feng's Blog Theme by OneBlog
    搜索
    标签
    # 随笔 # 维修 # Wordpress # 阿里云 # 服务器 # typecho # Ghost # iphone # 网站 # word
  • 首页>
  • IT网络>
  • 正文
  • Typecho 博客评论显示 UserAgent (UA)

    2020年03月04日 98 阅读 0 评论 5864 字

    本篇文章纯搬运 左岸大佬 的教程,之所以搬运也是为了记录下过程,也便于以后时间长了还能再参考着折腾(没办法,文章写不出来多少,折腾这些东西无用的却是十分的来劲)废话不多说,赶紧码字+拷贝记录下来。

    由于目前我用的是Mirages主题,所以接下来也按这个主题来:

    1. 将下面的样式表外链加入到/usr/themes/Mirages/component/header.php的head部分,当然也可通过主题设置界面添加,这里建议大伙把css中的图片下载到本地,以便将来…………。
    <link rel="stylesheet" href="//cdn.zrahh.com/css/comment-ua.css">
    

    1. 将以下代码加入到/usr/themes/Mirages/function.php末尾
    // 获取浏览器信息
    function getBrowser($agent)
    {
        if (preg_match('/MSIE\s([^\s|;]+)/i', $agent, $regs)) {
            $outputer = '<i class="ua-icon icon-ie"></i>  Internet Explore';
        } else if (preg_match('/FireFox\/([^\s]+)/i', $agent, $regs)) {
          $str1 = explode('Firefox/', $regs[0]);
    $FireFox_vern = explode('.', $str1[1]);
            $outputer = '<i class="ua-icon icon-firefox"></i>  FireFox';
        } else if (preg_match('/Maxthon([\d]*)\/([^\s]+)/i', $agent, $regs)) {
          $str1 = explode('Maxthon/', $agent);
    $Maxthon_vern = explode('.', $str1[1]);
            $outputer = '<i class="ua-icon icon-edge"></i>  MicroSoft Edge';
        } else if (preg_match('#360([a-zA-Z0-9.]+)#i', $agent, $regs)) {
    $outputer = '<i class="ua-icon icon-360"></i>  360极速浏览器';
        } else if (preg_match('/Edge([\d]*)\/([^\s]+)/i', $agent, $regs)) {
            $str1 = explode('Edge/', $regs[0]);
    $Edge_vern = explode('.', $str1[1]);
            $outputer = '<i class="ua-icon icon-edge"></i>  MicroSoft Edge';
        } else if (preg_match('/UC/i', $agent)) {
                  $str1 = explode('rowser/',  $agent);
    $UCBrowser_vern = explode('.', $str1[1]);
            $outputer = '<i class="ua-icon icon-uc"></i>  UC浏览器';
        }  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 = '<i class= "ua-icon icon-qq"></i>  QQ浏览器';
        } else if (preg_match('/UBrowser/i', $agent, $regs)) {
                  $str1 = explode('rowser/',  $agent);
    $UCBrowser_vern = explode('.', $str1[1]);
            $outputer = '<i class="ua-icon icon-uc"></i>  UC浏览器';
        }  else if (preg_match('/Opera[\s|\/]([^\s]+)/i', $agent, $regs)) {
            $outputer = '<i class= "ua-icon icon-opera"></i>  Opera';
        } else if (preg_match('/Chrome([\d]*)\/([^\s]+)/i', $agent, $regs)) {
    $str1 = explode('Chrome/', $agent);
    $chrome_vern = explode('.', $str1[1]);
            $outputer = '<i class="ua-icon icon-chrome"></i>  Google Chrome';
        } else if (preg_match('/safari\/([^\s]+)/i', $agent, $regs)) {
             $str1 = explode('Version/',  $agent);
    $safari_vern = explode('.', $str1[1]);
            $outputer = '<i class="ua-icon icon-safari"></i>  Safari';
        } else{
            $outputer = '<i class="ua-icon icon-chrome"></i>  Google Chrome';
        }
        echo $outputer;
    }
    // 获取操作系统信息
    function getOs($agent)
    {
        $os = false;
    
        if (preg_match('/win/i', $agent)) {
            if (preg_match('/nt 6.0/i', $agent)) {
                $os = '  <i class= "ua-icon icon-win1"></i>  Windows Vista / ';
            } else if (preg_match('/nt 6.1/i', $agent)) {
                $os = '  <i class= "ua-icon icon-win1"></i>  Windows 7 / ';
            } else if (preg_match('/nt 6.2/i', $agent)) {
                $os = '  <i class="ua-icon icon-win2"></i>  Windows 8 / ';
            } else if(preg_match('/nt 6.3/i', $agent)) {
                $os = '  <i class= "ua-icon icon-win2"></i>  Windows 8.1 / ';
            } else if(preg_match('/nt 5.1/i', $agent)) {
                $os = '  <i class="ua-icon icon-win1"></i>  Windows XP / ';
            } else if (preg_match('/nt 10.0/i', $agent)) {
                $os = '  <i class="ua-icon icon-win2"></i>  Windows 10 / ';
            } else{
                $os = '  <i class="ua-icon icon-win2"></i>  Windows X64 / ';
            }
        } else if (preg_match('/android/i', $agent)) {
        if (preg_match('/android 9/i', $agent)) {
                $os = '  <i class="ua-icon icon-android"></i>  Android Pie / ';
            }
        else if (preg_match('/android 8/i', $agent)) {
                $os = '  <i class="ua-icon icon-android"></i>  Android Oreo / ';
            }
        else{
                $os = '  <i class="ua-icon icon-android"></i>  Android / ';
        }
        }
        else if (preg_match('/ubuntu/i', $agent)) {
            $os = '  <i class="ua-icon icon-ubuntu"></i>  Ubuntu / ';
        } else if (preg_match('/linux/i', $agent)) {
            $os = '  <i class= "ua-icon icon-linux"></i>  Linux / ';
        } else if (preg_match('/iPhone/i', $agent)) {
            $os = '  <i class="ua-icon icon-apple"></i>  iPhone / ';
        } else if (preg_match('/mac/i', $agent)) {
            $os = '  <i class="ua-icon icon-mac"></i>  MacOS / ';
        }else if (preg_match('/fusion/i', $agent)) {
            $os = '  <i class="ua-icon icon-android"></i>  Android / ';
        } else {
            $os = '  <i class="ua-icon icon-linux"></i>  Linux / ';
        }
        echo $os;
    }
    
    1. 将以下代码添加到/usr/themes/Mirages/lib/comments.php中122行所在的div中
    <span class="comment-ua">
        <?php getOs($this->agent); ?>
        <?php getBrowser($this->agent); ?>
    </span>
    

    本文部分内容转载自 LOGI ,如果你有缘看到这篇文章,也去踩踩他的博客吧。

    本文著作权归作者 [ Feng ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
    typecho
    — END —
    首页
    Copyright©2012-2025  All Rights Reserved.  Load:0.015 s
    Theme by OneBlog V3.6.3
    夜间模式

    开源不易,请尊重作者版权,保留基本的版权信息。