Ua.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wangfanchang
  5. * Date: 18/1/10
  6. * Time: 下午2:13
  7. */
  8. namespace app\common\library;
  9. class Ua
  10. {
  11. /**
  12. * user agent
  13. */
  14. private static $ua = null;
  15. /**
  16. * 获取用户完整user agent
  17. * @return string
  18. */
  19. public static function getUa()
  20. {
  21. if (is_null(self::$ua)) {
  22. self::$ua = $_SERVER['HTTP_USER_AGENT'] ?? ''; //存在无UA的情况,如:微信callback post
  23. }
  24. return self::$ua;
  25. }
  26. /**
  27. * 获取NetType网络类型
  28. * @return string
  29. */
  30. public static function getNetType()
  31. {
  32. foreach (['WIFI', '4G', '3G', '2G'] as $key => $value) {
  33. if (stripos(self::getUa(), $value) !== false) {
  34. return $value;
  35. }
  36. }
  37. return 'Other';
  38. }
  39. /**
  40. * 获取OS环境(Ios,Android,Other)
  41. */
  42. public static function getOs()
  43. {
  44. if (self::isIos()) {
  45. return 'iOS';
  46. } elseif (self::isAndroid()) {
  47. return 'Android';
  48. } else {
  49. return 'Other';
  50. }
  51. }
  52. /**
  53. * 是否QQ环境
  54. * @return bool
  55. */
  56. public static function isQQ()
  57. {
  58. return stripos(self::getUa(), ' QQ/') !== false;
  59. }
  60. /**
  61. * 是否微信环境
  62. * @return bool
  63. */
  64. public static function isWeiXin()
  65. {
  66. return stripos(self::getUa(), 'MicroMessenger') !== false;
  67. }
  68. /**
  69. * 是否Android环境
  70. * @return bool
  71. */
  72. public static function isAndroid()
  73. {
  74. return stripos(self::getUa(), 'Android') !== false;
  75. }
  76. /**
  77. * 是否Ios环境
  78. * @return bool
  79. */
  80. public static function isIos()
  81. {
  82. return preg_match("/iPhone|iPad|iPod/i",self::getUa());
  83. }
  84. /**
  85. * 是否hua wei 手机环境
  86. * @return bool
  87. */
  88. public static function isHuaWei()
  89. {
  90. return preg_match("/build\/huawei|build\/honor|build\/hdh/i", self::getUa());
  91. }
  92. }