當前位置:首頁 >  站長 >  編程技術(shù) >  正文

Typecho實現(xiàn)評論顯示UserAgent,操作系統(tǒng)和瀏覽器標識

 2020-11-05 13:38  來源: 林三在線   我來投稿 撤稿糾錯

  域名預(yù)訂/競價,好“米”不錯過

最近更新友情鏈接,突然羨慕別人有的瀏覽器標識,能看到更多的評論者信息,我覺得能夠增加訪客在頁面停留的時間吧,所以決定把自己的typecho也實現(xiàn)這個功能,通過對比添加函數(shù)和使用插件兩種方法,有了心得。

由于想給博客加一個文章評論顯示 UserAgent 功能,在網(wǎng)上搜尋并嘗試了 UserAgent 插件,但是并不如意。一是因為他太過臃腫,雖然幾乎可以識別市面上所有的 OS 和瀏覽器,但是我們常用的也就幾個而已,大多數(shù)都用不到,二是圖標太老舊了,而且清晰度很低,在博客上顯得突兀且不美觀。----來自左岸博客的引用(傳送門)

代碼實現(xiàn)評論顯示UserAgent

林三通過試用,覺得左岸同學的方法比較適合自己,因為手動添加函數(shù)意味著更自由的位置和樣式顯示。

1、首先找到主題根目錄,打開 functions.php 文件,在函數(shù)區(qū)域(不是最頂端)粘貼下面的代碼:

// 獲取瀏覽器信息

function getBrowser($agent)

{

if (preg_match('/MSIE\s([^\s|;]+)/i', $agent, $regs)) {

$outputer = 'Internet Explore';

} else if (preg_match('/FireFox\/([^\s]+)/i', $agent, $regs)) {

$str1 = explode('Firefox/', $regs[0]);

$FireFox_vern = explode('.', $str1[1]);

$outputer = 'FireFox';

} else if (preg_match('/Maxthon([\d]*)\/([^\s]+)/i', $agent, $regs)) {

$str1 = explode('Maxthon/', $agent);

$Maxthon_vern = explode('.', $str1[1]);

$outputer = 'MicroSoft Edge';

} else if (preg_match('#360([a-zA-Z0-9.]+)#i', $agent, $regs)) {

$outputer = '360 Fast Browser';

} else if (preg_match('/Edge([\d]*)\/([^\s]+)/i', $agent, $regs)) {

$str1 = explode('Edge/', $regs[0]);

$Edge_vern = explode('.', $str1[1]);

$outputer = 'MicroSoft Edge';

} else if (preg_match('/UC/i', $agent)) {

$str1 = explode('rowser/', $agent);

$UCBrowser_vern = explode('.', $str1[1]);

$outputer = 'UC Browser';

} else if (preg_match('/QQ/i', $agent, $regs)||preg_match('/QQ Browser\/([^\s]+)/i', $agent, $regs)) {

$str1 = explode('rowser/', $agent);

$QQ_vern = explode('.', $str1[1]);

$outputer = 'QQ Browser';

} else if (preg_match('/UBrowser/i', $agent, $regs)) {

$str1 = explode('rowser/', $agent);

$UCBrowser_vern = explode('.', $str1[1]);

$outputer = 'UC Browser';

} else if (preg_match('/Opera[\s|\/]([^\s]+)/i', $agent, $regs)) {

$outputer = 'Opera';

} else if (preg_match('/Chrome([\d]*)\/([^\s]+)/i', $agent, $regs)) {

$str1 = explode('Chrome/', $agent);

$chrome_vern = explode('.', $str1[1]);

$outputer = 'Google Chrome';

} else if (preg_match('/safari\/([^\s]+)/i', $agent, $regs)) {

$str1 = explode('Version/', $agent);

$safari_vern = explode('.', $str1[1]);

$outputer = 'Safari';

} else{

$outputer = 'Google Chrome';

}

echo $outputer;

}

// 獲取操作系統(tǒng)信息

function getOs($agent)

{

$os = false;

if (preg_match('/win/i', $agent)) {

if (preg_match('/nt 6.0/i', $agent)) {

$os = 'Windows Vista · ';

} else if (preg_match('/nt 6.1/i', $agent)) {

$os = 'Windows 7 · ';

} else if (preg_match('/nt 6.2/i', $agent)) {

$os = 'Windows 8 · ';

} else if(preg_match('/nt 6.3/i', $agent)) {

$os = 'Windows 8.1 · ';

} else if(preg_match('/nt 5.1/i', $agent)) {

$os = 'Windows XP · ';

} else if (preg_match('/nt 10.0/i', $agent)) {

$os = 'Windows 10 · ';

} else{

$os = 'Windows X64 · ';

}

} else if (preg_match('/android/i', $agent)) {

if (preg_match('/android 9/i', $agent)) {

$os = 'Android Pie · ';

}

else if (preg_match('/android 8/i', $agent)) {

$os = 'Android Oreo · ';

}

else{

$os = 'Android · ';

}

}

else if (preg_match('/ubuntu/i', $agent)) {

$os = 'Ubuntu · ';

} else if (preg_match('/linux/i', $agent)) {

$os = 'Linux · ';

} else if (preg_match('/iPhone/i', $agent)) {

$os = 'iPhone · ';

} else if (preg_match('/mac/i', $agent)) {

$os = 'MacOS · ';

}else if (preg_match('/fusion/i', $agent)) {

$os = 'Android · ';

} else {

$os = 'Linux · ';

}

echo $os;

}

2、comments.php 中找到合適位置(比如評論作者的后面)添加以下代碼:

agent); ?>agent); ?>

然后刷新頁面就可以看到UA信息顯示出來了。林三這里去掉了圖標顯示,如果想要圖標的,請參考左岸博客的原文來操作,還需要復(fù)制相應(yīng)的css代碼到你的樣式表中(左岸同學使用的圖標在cnd上面,速度還是不錯的)。

插件實現(xiàn)評論顯示UserAgent

UserAgent是一個較好的評論增強插件,可以分別設(shè)置圖標、文字或圖標+文字形式,來顯示評論的UA部分。

1、下載插件:https://github.com/ennnnny/typecho 或者使用網(wǎng)盤下載:傳送門,提取碼 aqe5

2、然后解壓上傳UserAgent文件夾至主題插件目錄并啟用,選擇你想要的顯示效果保存

3、引用,在你想顯示的位置上加上這段代碼:agent); ?>

請根據(jù)自己的模板來判斷是使用$this或$comments(如果不清楚,可以都試下),林三試了下,也是可以顯示的。

文章來源:林三在線

來源地址:https://linsan.net/typecho%e5%ae%9e%e7%8e%b0%e8%af%84%e8%ae%ba%e6%98%be%e7%a4%bauseragent%ef%bc%8c%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9f%e5%92%8c%e6%b5%8f%e8%a7%88%e5%99%a8%e6%a0%87%e8%af%86.html

申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!

相關(guān)文章

熱門排行

信息推薦