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


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

获取用户IP

  • 如果使用的是Typecho系统,那么评论里是可以直接获取到评论IP的。
$comments->ip
  • 非Typecho系统,php语言则使用以下代码。
//获取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数据的。
$comments->agent
  • 非Typecho系统,php语言则使用以下代码。
$agent = $_SERVER['HTTP_USER_AGENT'];

UserAgent信息处理

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

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

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

大功告成

刷新页面,查看。

UserAgent.php源码

<?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;
        }
    }
}

声明:目的地-Destination|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - 如何让文章显示用户评论时所用的设备是什么


前程似锦、未来可期、寻得良人、共赴白头,祝你也祝我。