前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何让文章显示用户评论时所用的设备是什么

如何让文章显示用户评论时所用的设备是什么

作者头像
目的地-Destination
发布2023-03-06 16:39:01
2.1K0
发布2023-03-06 16:39:01
举报
文章被收录于专栏:目的地-Destination

怎样让文章的评论显示出用户评论时,使用的是什么设备呢?

获取用户IP

  • 如果使用的是Typecho系统,那么评论里是可以直接获取到评论IP的。
代码语言:javascript
复制
$comments->ip
  • 非Typecho系统,php语言则使用以下代码。
代码语言:javascript
复制
//获取IP
function getIp(){
    $ip= '-';
    if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])
    {
      $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
    }
    elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
    {
      $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
    }
    elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])
    {
      $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
    }
    elseif (getenv("HTTP_X_FORWARDED_FOR"))
    {
      $ip = getenv("HTTP_X_FORWARDED_FOR");
    }
    elseif (getenv("HTTP_CLIENT_IP"))
    {
      $ip = getenv("HTTP_CLIENT_IP");
    }
    elseif (getenv("REMOTE_ADDR"))
    {
      $ip = getenv("REMOTE_ADDR");
    }
    else
    {
      $ip = "Unknown";
    }
    return $ip;
}

获取请求头agent

  • 如果使用的是Typecho系统,那么评论里也是可以直接获取到agent数据的。
代码语言:javascript
复制
$comments->agent
  • 非Typecho系统,php语言则使用以下代码。
代码语言:javascript
复制
$agent = $_SERVER['HTTP_USER_AGENT'];

UserAgent信息处理

  • 新建一个php文件UserAgent.php,放在当前模板文件夹下。内容见下方代码。
  • 在评论文件内引用:<?php require_once 'UserAgent.php';?>
  • 将UserAgent实例化,并传入用户评论时的agent值。

  Typecho系统:<?php ua = new UserAgent(agent);?>

  • 调用获取结果:<?php echo "发自" . $ua->returnTimeUa()['title'];?>

大功告成

刷新页面,查看。

UserAgent.php源码

代码语言:javascript
复制
<?php
/**
 * UserAgent
 * @author Chrison
 * @package custom
 */
class UserAgent{

    public $ua;

    public function __construct($ua = '')
    {
        $this->ua = $ua;
    }

    public function returnOS(){
        $ua = $this->ua;
        $title = "未知浏览器";
        if (preg_match('/win/i', $ua)) {
            if (preg_match('/Windows NT 6.1/i', $ua)) {
                $title = "Windows 7";
            }elseif (preg_match('/Windows 98/i', $ua)) {
                $title = "Windows 98";
            }elseif (preg_match('/Windows NT 5.0/i', $ua)) {
                $title = "Windows 2000";
            }elseif (preg_match('/Windows NT 5.1/i', $ua)) {
                $title = "Windows XP";
            }elseif (preg_match('/Windows NT 5.2/i', $ua)) {
                if (preg_match('/Win64/i', $ua)) {
                    $title = "Windows XP 64 bit";
                } else {
                    $title = "Windows Server 2003";
                }
            }elseif (preg_match('/Windows NT 6.0/i', $ua)) {
                $title = "Windows windows";
            }elseif (preg_match('/Windows NT 6.2/i', $ua)) {
                $title = "Windows 8";
            }elseif (preg_match('/Windows NT 6.3/i', $ua)) {
                $title = "Windows 8.1";
            }elseif (preg_match('/Windows NT 10.0/i', $ua)) {
                $title = "Windows 10";
            }elseif (preg_match('/Windows Phone/i', $ua)) {
                $matches = explode(';',$ua);
                $title = $matches[2];
            }
        } elseif (preg_match('#iPod.*.CPU.([a-zA-Z0-9.( _)]+)#i', $ua, $matches)) {
            $title = "iPod ";//.$matches[1]
        } elseif (preg_match('/iPhone OS ([_0-9]+)/i', $ua, $matches)) {
            $title = "Iphone ";//.$matches[1]
        } elseif (preg_match('/iPad; CPU OS ([_0-9]+)/i', $ua, $matches)) {
            $title = "iPad ";//.$matches[1]
        } elseif (preg_match('/Mac OS X ([0-9_]+)/i', $ua, $matches)) {
            if(count(explode(7,$matches[1]))>1) $matches[1] = 'Lion '.$matches[1];
            elseif(count(explode(8,$matches[1]))>1) $matches[1] = 'Mountain Lion '.$matches[1];
            $title = "Mac OSX";
        } elseif (preg_match('/Macintosh/i', $ua)) {
            $title = "Mac OS";
        } elseif (preg_match('/CrOS/i', $ua)){
            $title = "Google Chrome OS";
        } elseif (preg_match('/Linux/i', $ua)) {
            $title = 'Linux';
            if (preg_match('/Ubuntu/i', $ua)) {
                $title = "Ubuntu Linux";
            }elseif(preg_match('#Debian#i', $ua)) {
                $title = "Debian GNU/Linux";
            }elseif (preg_match('#Fedora#i', $ua)) {
                $title = "Fedora Linux";
            }elseif (preg_match('/Kraitnabo\/([^\s|;]+)/i', $ua, $matches)) {
            $title = '南博app '. $matches[1];
            }elseif (preg_match('/Android.([0-9. _]+)/i',$ua, $matches)) {
                $title= "Android";
            }
        } elseif (preg_match('/Android.([0-9. _]+)/i',$ua, $matches)) {
            $title= "Android";
        } elseif (preg_match('/siri/i',$ua, $matches)) {
            $title= "iOS Siri快捷指令";
        }
        return array("title"=>$title);
    }

    /**
     * 还可以自行扩展
     */
    public function returnTimeUa(){
        if ($this->ua == "weixin" || $this->ua == "weChat"){
            return array("title"=>("微信公众号"));
        }elseif ($this->ua == "crx"){
            return array("title"=>("Chrome扩展"));
        }elseif ($this->ua == "yearcross"){
            return array("title"=>("YearCross"));
        }elseif ($this->ua == "Kraitnabo"){
            return array("title"=>("南博app"));
        }elseif ($this->ua == "python"){
            return array("title"=>("python脚本"));
        }else{
            $ua = $this->returnOS();
            return $ua;
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023年02月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 获取用户IP
  • 获取请求头agent
  • UserAgent信息处理
  • 大功告成
  • UserAgent.php源码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档