Wodpress评论免插件显示UA信息

Wodpress评论免插件显示UA信息
1.自己查找这方面资料很多,折腾了不少时间还是选择了这种方法,如果嫌麻烦可以直接使用WP-UserAgent插件,插件使用更简单,直接在博客后台搜索安装插件即可完成。
2.代码法
原理:
通过分析留言中存储的userAgent信息分析操作系统和浏览器
例如:userAgent信息为Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36

创建userAgent.php文件,代码:

//管理员判断开始  
function sskadmin($e){  
    $ssk='';  
    if($e==1){  
        if(sskcheckMobile()){  
            $ssk = '<span class="user-level-gw user-admin-gw user-admin-gw" title="管理员"><i>管理员</i></span>';  
        }else{  
            $ssk = '<div style="position: absolute;margin-left:300px;margin-top:-75px;filter:alpha(opacity=50); -moz-opacity:0.15; -khtml-opacity:0.15; opacity:0.15;">';  
            $ssk .='<img src="./wp-content/themes/dux/img/adminreply.png" width="440" height="150"></div>';  
        }  
    }else{  
        if(sskcheckMobile()){  
          $ssk = '';  
        }  
    }  
     return $ssk;  
}  
//检查是否是移动客户端  
function sskcheckMobile(){  
     $Agent = $_SERVER['HTTP_USER_AGENT'];  
     $rul='/iPad/i';  
      $isiPad= preg_match($rul,$Agent)!=null;  
    if($isiPad){  
        return false;  
    }  
    $mobile='/iphone|android|phone|mobile|wap|netfront|x11|java|opera mobi|opera mini|ucweb|windows ce|symbian|symbianos|series|webos|sony|blackberry|dopod|nokia|samsung|palmsource|xda|pieplus|meizu|midp|cldc|motorola|foma|docomo|up.browser|up.link|blazer|helio|hosin|huawei|novarra|coolpad|webos|techfaith|palmsource|alcatel|amoi|ktouch|nexian|ericsson|philips|sagem|wellcom|bunjalloo|maui|smartphone|iemobile|spice|bird|zte-|longcos|pantech|gionee|portalmmm|jig browser|hiptop|benq|haier|^lct|320x320|240x320|176x220/i';  
    $isMobile=preg_match($mobile,$Agent)!=null;  
    if($isMobile){  
        return true;  
    }  
    return false;  
  
}  
//显示UA  
function sskua($e) {  
    $r = Array();  
    $outputer = '';  
    if ($r = preg_match("/FireFox\/([^\s]+)/i",$e,$match)) {  
        $string=explode('/', $match[0]);  
        $outputer = '<span class="ua_firefox"><i class="fa fa-globe"></i> Mozilla FireFox'  .'|' . $string[1];  
  
    }else if ($r = preg_match("/Maxthon([\d]*)\/([^\s]+)/i",$e,$match)) {  
        $string=explode('/', $match[0]);  
        $outputer = '<span class="ua_maxthon"><i class="fa fa-globe"></i> Maxthon';  
  
    }else if ($r = preg_match("/BIDUBrowser([\d]*)\/([^\s]+)/i",$e,$match)) {  
        $string=explode('/', $match[0]);  
        $outputer = '<span class="ua_ucweb"><i class="fa fa-globe"></i> 百度浏览器' .'|' . $string[1];  
  
    } else if ($r = preg_match("/UBrowser([\d]*)\/([^\s]+)/i",$e,$match)) {  
        $string=explode('/', $match[0]);  
        $outputer = '<span class="ua_ucweb"><i class="fa fa-globe"></i> UCBrowser'  .'|' . $string[1];  
    } else if ($r = preg_match("/UCBrowser([\d]*)\/([^\s]+)/i",$e,$match)) {  
        $string=explode('/', $match[0]);  
        $outputer = '<span class="ua_ucweb"><i class="fa fa-globe"></i> UCBrowser'.'|' . $string[1];  
    }else if ($r = preg_match("/MetaSr/i",$e,$match)) {  
        $outputer = '<span class="ua_sogou"><i class="fa fa-globe"></i> 搜狗浏览器';  
    }else if ($r = preg_match("/2345Explorer/i",$e,$match)) {  
        $outputer = '<span class="ua_2345explorer"><a href="" target="_blank" style="color:#FFFFFF!important;"><i class="fa fa-globe"></i> 2345王牌浏览器</a>';  
    }else if ($r = preg_match("/2345chrome/i",$e,$match)) {  
        $outputer = '<span class="ua_2345chrome"><a href="" target="_blank" style="color:#FFFFFF!important;"><i class="fa fa-globe"></i> 2345加速浏览器</a>';  
    }else if ($r = preg_match("/LBBROWSER/i",$e,$match)) {  
        $outputer = '<span class="ua_lbbrowser"><i class="fa fa-globe"></i> 猎豹安全浏览器';  
  
    } else if ($r = preg_match("/MicroMessenger\/([^\s]+)/i",$e,$match)) {  
        $string=explode('/', $match[0]);  
        $outputer = '<span class="ua_qq"><i class="fa fa-weixin"></i> 微信' .'|' . $string[1];  
    }else if ($r = preg_match("/QQBrowser\/([^\s]+)/i",$e,$match)) {  
        $string=explode('/', $match[0]);  
        $outputer = '<span class="ua_qq"><i class="fa fa-globe"></i> QQ浏览器'  .'|' . $string[1];  
    } else if ($r = preg_match("/QQ\/([^\s]+)/i",$e,$match)) {  
        $string=explode('/', $match[0]);  
        $outputer = '<span class="ua_qq"><i class="fa fa-globe"></i> QQ浏览器' .'|' . $string[1];  
    }else if ($r = preg_match("/MiuiBrowser\/([^\s]+)/i",$e,$match)) {  
        $string=explode('/', $match[0]);  
        $outputer = '<span class="ua_mi"><i class="fa fa-globe"></i> Miui浏览器' .'|' . $string[1];  
    }else if ($r = preg_match("/Chrome([\d]*)\/([^\s]+)/i",$e,$match)) {  
        $string=explode('/', $match[0]);  
        $outputer = '<span class="ua_chrome"><i class="fa fa-globe"></i> Chrome' .'|' . $string[1];  
    }else if ($r = preg_match("/safari\/([^\s]+)/i",$e,$match)) {  
        $string=explode('/', $match[0]);  
        $outputer = '<span class="ua_apple"><i class="fa fa-globe"></i> Apple Safari'.'|' . $string[1];  
    }else if ($r = preg_match("/Opera[\s|\/]([^\s]+)/i",$e,$match)) {  
        $string=explode('/', $match[0]);  
        $outputer = '<span class="ua_opera"><i class="fa fa-globe"></i> Opera' .'|' . $string[1];  
    }else if ($r = preg_match("/Trident\/7.0/i",$e,$match)) {  
        $outputer = '<span class="ua_ie"><i class="fa fa-globe"></i> Internet Explorer 11';  
    } else if ($r = preg_match("/MSIE\s([^\s|;]+)/i",$e,$match)) {  
        $outputer = '<span class="ua_ie"><i class="fa fa-globe"></i> Internet Explorer' .'|'.$match[0];  
    }else {  
        $outputer = '<span class="ua_other"><i class="fa fa-globe"></i> 其它浏览器';  
    }  
    // mobile style  
    if(sskcheckMobile()){  
        $Mobile='<br>';  
        $Mobiletop='<br>';  
  
    }else{  
        $Mobile='';  
    }  
    return $Mobiletop."<span class='ua'>".$outputer."</span></span>".$Mobile ;  
}  
function sskos($e) {  
    $os = '';  
    if (preg_match("/win/i",$e,$match)) {  
        if(preg_match("/nt 5.1/i",$e,$match)){  
            $os = '<span class="os_xp"><i class="fa fa-desktop"></i> Windows XP';  
  
        } else if (preg_match("/nt 6.1/i",$e,$match)) {  
            $os = '<span class="os_7"><i class="fa fa-desktop"></i> Windows 7';  
  
        }else if (preg_match("/nt 6.2/i",$e,$match) ) {  
            $os = '<span class="os_8"><i class="fa fa-desktop"></i> Windows 8';  
  
        }else if (preg_match("/nt 6.3/i",$e,$match)) {  
            $os = '<span class="os_8_1"><i class="fa fa-desktop"></i> Windows 8.1';  
  
        }else if (preg_match("/nt 10.0/i",$e,$match)) {  
            $os = '<span class="os_8_1"><i class="fa fa-desktop"></i> Windows 10';  
        }else if (preg_match("/nt 6.0/i",$e,$match)) {  
            $os = '<span class="os_vista"><i class="fa fa-desktop"></i> Windows Vista';  
        }else if (preg_match("/nt 5/i",$e,$match)) {  
            $os = '<span class="os_2000"><i class="fa fa-desktop"></i> Windows 2000';  
        } else {  
            $os = '<span class="os_windows"><i class="fa fa-desktop"></i> Windows';  
        }  
    }else if (preg_match("/android/i",$e,$match)) {  
        $os = '<span class="os_android"><i class="fa fa-android"></i> Android';  
    }else if (preg_match("/ubuntu/i",$e,$match)) {  
        $os = '<span class="os_ubuntu"><i class="fa fa-desktop"></i> Ubuntu';  
    }else if (preg_match("/linux/i",$e,$match)) {  
        $os = '<span class="os_linux"><i class="fa fa-linux"></i> Linux';  
    } else if (preg_match("/mac/i",$e,$match)) {  
        $os = '<span class="os_mac"><i class="fa fa-desktop"></i> Mac OS X';  
    } else if (preg_match("/unix/i",$e,$match)) {  
        $os = '<span class="os_unix"><i class="fa fa-desktop"></i> Unix';  
    } else if (preg_match("/symbian/i",$e,$match)) {  
        $os = '<span class="os_nokia"><i class="fa fa-mobile"></i> Nokia SymbianOS';  
    } else {  
        $os = '<span class="os_other"><i class="fa fa-desktop"></i> 其它操作系统';  
    }  
    return "<span class='ua'>".$os."</span></span>";  
}  

第一步,以上代码是根据多说中改制而成的。目前以上代码判断的浏览器类型还是比较准确无误的。
编辑主题内文件:functions.php
?>前写入:
//显示访客信息
include("userAgent.php");

第二步,在评论文件中调用:

echo sskadmin($comment->user_id);
echo sskua($comment->comment_agent);
echo sskos($comment->comment_agent);

将上面的三行代码,复制到mo_comments_list.php文件中,这个文件 主要就是来显示我们用户评论的模块列表的。具体位置可以根据自己的主题样式来调整。

第三步:添加css样式
有能力的可以自己写,这里给出一个范例:

/*浏览器和系统显示*/  
:before,:after {  
    -webkit-box-sizing: border-box;  
    -moz-box-sizing: border-box;  
    box-sizing: border-box  
}  
span.ua{  
/*.comt-main{}*/  
 font-size: 12px;  
 color:#888;  
 word-wrap: break-word;   
 line-height:22px;  
   
 margin: 0 1px!important;  
 color:#FFFFFF!important;  
   
 /*text-transform: Capitalize!important; 
 float: right!important; 
 line-height: 18px!important;*/  
}  
.ua_other.os_other{  
 background-color: #ccc!important;  
 color: #fff;  
 border: 1px solid #BBB!important;  
 border-radius: 4px;  
}  
.ua_ie{  
 background-color: #428bca!important;  
 border-color: #357ebd!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.ua_firefox{  
 background-color: #f0ad4e!important;  
 border-color: #eea236!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.ua_maxthon{  
 background-color: #7373B9!important;  
 border-color: #7373B9!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.ua_ucweb{  
 background-color: #FF740F!important;  
 border-color: #d43f3a!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.ua_sogou{  
 background-color: #78ACE9!important;  
 border-color: #4cae4c!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.ua_2345explorer{  
 background-color: #2478B8!important;  
 border-color: #4cae4c!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.ua_2345chrome{  
 background-color: #F9D024!important;  
 border-color: #4cae4c!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.ua_mi{  
 background-color: #FF4A00!important;  
 border-color: #4cae4c!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.ua_lbbrowser{  
 background-color: #FC9D2E!important;  
 border-color: #4cae4c!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.ua_chrome{  
 background-color: #EE6252!important;  
 border-color: #4cae4c!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.ua_qq{  
 background-color: #3D88A8!important;  
 border-color: #4cae4c!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.ua_apple{  
 background-color: #E95620!important;  
 border-color: #4cae4c!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.ua_opera{  
 background-color: #d9534f!important;  
 border-color: #d43f3a!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
  
.ua_other{  
 background-color: #d9534f!important;  
 border-color: #d43f3a!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
   
   
.os_vista,.os_2000,.os_windows,.os_xp,.os_7,.os_8,.os_8_1,.os_other {  
 background-color: #39b3d7!important;  
 border-color: #46b8da!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
   
.os_android {  
 background-color: #98C13D!important;  
 border-color: #01B171!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.os_ubuntu{  
 background-color: #DD4814!important;  
 border-color: #01B171!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.os_linux {  
 background-color: #3A3A3A!important;  
 border-color: #1F1F1F!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.os_mac{  
 background-color: #666666!important;  
 border-color: #1F1F1F!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.os_unix{  
 background-color: #006600!important;  
 border-color: #1F1F1F!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
.os_nokia{  
 background-color: #014485!important;  
 border-color: #1F1F1F!important;  
 border-radius: 4px;  
 padding: 0 5px!important;  
}  
/*user_agent*  

css样式可以另行调用,也可以放到其他css内合并,合并时注意不要css命名冲突和重复。

以上两处代码就可以实现,简单吧,也不用使用任何的插件。代码就是如此的强大。而且还能保证访问的速度哦!



参考文章:
Wodpress评论免插件显示UA信息
http://www.phpydt.com/387.html
wordpress显示评论者地理位置信息
http://www.phpydt.com/395.html
Wodpress评论UA信息和地理信息显示(代码,免插件)
http://t.cn/RZYnuo6
两种方法实现评论显示UV标识,浏览器、系统、国家

 继续浏览关于 技术有趣WordPress插件 的文章

 本文最后更新于 2019/06/23 14:23:52,可能因经年累月而与现状有所差异

 引用转载请注明:icret > 教程 > Wodpress评论免插件显示UA信息