Agent.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * tpAdmin [a web admin based ThinkPHP5]
  4. *
  5. * @author yuan1994 <tianpian0805@gmail.com>
  6. * @link http://tpadmin.yuan1994.com/
  7. * @copyright 2016 yuan1994 all rights reserved.
  8. * @license http://www.apache.org/licenses/LICENSE-2.0
  9. */
  10. //------------------------
  11. // 根据user-agent获取浏览器版本,操作系统
  12. //-------------------------
  13. class Agent
  14. {
  15. public static function getVesion($v){
  16. if(isset($v[1])){
  17. $version= $v[1];
  18. }else{
  19. $version=0;
  20. }
  21. return $version;
  22. }
  23. /**
  24. * 获取客户端浏览器信息 添加win10 edge浏览器判断
  25. * @param null
  26. * @author Jea杨
  27. * @return string
  28. */
  29. public static function getBroswer()
  30. {
  31. $sys = $_SERVER['HTTP_USER_AGENT']; //获取用户代理字符串
  32. if (stripos($sys, "Firefox/") > 0) {
  33. preg_match("/Firefox\/([^;)]+)+/i", $sys, $v);
  34. $exp[0] = "Firefox";
  35. $exp[1]=self::getVesion($v);
  36. } elseif (stripos($sys, "Maxthon") > 0) {
  37. preg_match("/Maxthon\/([\d\.]+)/", $sys, $v);
  38. $exp[0] = "傲游";
  39. $exp[1]=self::getVesion($v);
  40. } elseif (stripos($sys, "MSIE") > 0) {
  41. preg_match("/MSIE\s+([^;)]+)+/i", $sys, $v);
  42. $exp[0] = "IE";
  43. $exp[1]=self::getVesion($v);
  44. } elseif (stripos($sys, "OPR") > 0) {
  45. preg_match("/OPR\/([\d\.]+)/", $sys, $v);
  46. $exp[0] = "Opera";
  47. $exp[1]=self::getVesion($v);
  48. } elseif (stripos($sys, "Edge") > 0) {
  49. //win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配
  50. preg_match("/Edge\/([\d\.]+)/", $sys, $v);
  51. $exp[0] = "Edge";
  52. $exp[1]=self::getVesion($v);
  53. } elseif (stripos($sys, "Chrome") > 0) {
  54. preg_match("/Chrome\/([\d\.]+)/", $sys, $v);
  55. $exp[0] = "Chrome";
  56. $exp[1]=self::getVesion($v);
  57. } elseif (stripos($sys, 'rv:') > 0 && stripos($sys, 'Gecko') > 0) {
  58. preg_match("/rv:([\d\.]+)/", $sys, $v);
  59. $exp[0] = "IE";
  60. $exp[1]=self::getVesion($v);
  61. } elseif (stripos($sys, 'Safari') > 0) {
  62. preg_match("/safari\/([^\s]+)/i", $sys, $v);
  63. $exp[0] = "Safari";
  64. $exp[1]=self::getVesion($v);
  65. } else {
  66. $exp[0] = "未知浏览器";
  67. $exp[1] = "0";
  68. }
  69. return $exp[0] . '(' . $exp[1] . ')';
  70. }
  71. /**
  72. * 获取客户端操作系统信息包括win10
  73. * @param null
  74. * @author Jea杨
  75. * @return string
  76. */
  77. public static function getOs()
  78. {
  79. $agent = $_SERVER['HTTP_USER_AGENT'];
  80. if (preg_match('/win/i', $agent) && strpos($agent, '95')) {
  81. $os = 'Windows 95';
  82. } else if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90')) {
  83. $os = 'Windows ME';
  84. } else if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent)) {
  85. $os = 'Windows 98';
  86. } else if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent)) {
  87. $os = 'Windows Vista';
  88. } else if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent)) {
  89. $os = 'Windows 7';
  90. } else if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent)) {
  91. $os = 'Windows 8';
  92. } else if (preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent)) {
  93. $os = 'Windows 10';#添加win10判断
  94. } else if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent)) {
  95. $os = 'Windows XP';
  96. } else if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent)) {
  97. $os = 'Windows 2000';
  98. } else if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent)) {
  99. $os = 'Windows NT';
  100. } else if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent)) {
  101. $os = 'Windows 32';
  102. } else if (preg_match('/linux/i', $agent)) {
  103. $os = 'Linux';
  104. } else if (preg_match('/unix/i', $agent)) {
  105. $os = 'Unix';
  106. } else if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent)) {
  107. $os = 'SunOS';
  108. } else if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent)) {
  109. $os = 'IBM OS/2';
  110. } else if (preg_match('/Mac/i', $agent)) {
  111. $os = 'Mac';
  112. } else if (preg_match('/PowerPC/i', $agent)) {
  113. $os = 'PowerPC';
  114. } else if (preg_match('/AIX/i', $agent)) {
  115. $os = 'AIX';
  116. } else if (preg_match('/HPUX/i', $agent)) {
  117. $os = 'HPUX';
  118. } else if (preg_match('/NetBSD/i', $agent)) {
  119. $os = 'NetBSD';
  120. } else if (preg_match('/BSD/i', $agent)) {
  121. $os = 'BSD';
  122. } else if (preg_match('/OSF1/i', $agent)) {
  123. $os = 'OSF1';
  124. } else if (preg_match('/IRIX/i', $agent)) {
  125. $os = 'IRIX';
  126. } else if (preg_match('/FreeBSD/i', $agent)) {
  127. $os = 'FreeBSD';
  128. } else if (preg_match('/teleport/i', $agent)) {
  129. $os = 'teleport';
  130. } else if (preg_match('/flashget/i', $agent)) {
  131. $os = 'flashget';
  132. } else if (preg_match('/webzip/i', $agent)) {
  133. $os = 'webzip';
  134. } else if (preg_match('/offline/i', $agent)) {
  135. $os = 'offline';
  136. } elseif (preg_match('/ucweb|MQQBrowser|J2ME|IUC|3GW100|LG-MMS|i60|Motorola|MAUI|m9|ME860|maui|C8500|gt|k-touch|X8|htc|GT-S5660|UNTRUSTED|SCH|tianyu|lenovo|SAMSUNG/i', $agent)) {
  137. $os = 'mobile';
  138. } else {
  139. $os = '未知操作系统';
  140. }
  141. return $os;
  142. }
  143. }