DeviceDetectorTrait.php
7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 设备检测 trait
+-----------------------------------------------------------------------------------------------------------------------
*
* @copyright Copyright
* @author Richer
* @package App\Models\Traits
* @version 2019年11月13日,9:37:10
* @link
*/
namespace App\Models\Traits;
/**
* Trait DeviceDetectorTrait.
*/
trait DeviceDetectorTrait
{
/**
* 获取客户端的类型
* @return string
*/
public function getDeviceType()
{
if ($this->isMobile()) {
return 'mobile';
}
return 'pc';
}
/**
* 判断是否是移动端访问
* @return boolean
*/
public function isMobile()
{
// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
return true;
}
// 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
if (isset($_SERVER['HTTP_VIA'])) {
return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;// 找不到为flase,否则为TRUE
}
// 判断手机发送的客户端标志,兼容性有待提高
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$clientkeywords = array(
'mobile',
'nokia',
'sony',
'ericsson',
'mot',
'samsung',
'htc',
'sgh',
'lg',
'sharp',
'sie-',
'philips',
'panasonic',
'alcatel',
'lenovo',
'iphone',
'ipod',
'blackberry',
'meizu',
'android',
'netfront',
'symbian',
'ucweb',
'windowsce',
'palm',
'operamini',
'operamobi',
'openwave',
'nexusone',
'cldc',
'midp',
'wap'
);
// 从HTTP_USER_AGENT中查找手机浏览器的关键字
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
return true;
}
}
if (isset($_SERVER['HTTP_ACCEPT'])) { // 协议法,因为有可能不准确,放到最后判断
// 如果只支持wml并且不支持html那一定是移动设备
// 如果支持wml和html但是wml在html之前则是移动设备
if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
return true;
}
}
return false;
}
/**
* 获取操作系统版本
* @return string
*/
public function getPlatform()
{
$ua = $_SERVER['HTTP_USER_AGENT'];
if (strpos($ua, 'Android') !== false) {
preg_match("/(?<=Android)[\d\.]{1,}/", $ua, $version);
return 'Android';
} elseif (strpos($ua, 'iPhone') !== false) {
preg_match("/(?<=CPU iPhone OS)[\d\_]{1,}/", $ua, $version);
return 'iPhone';
} elseif (strpos($ua, 'iPad') !== false) {
preg_match("/(?<=CPU OS)[\d\_]{1,}/", $ua, $version);
return 'iPad';
}
}
/**
* 获取操作系统版本
* @return string
*/
public function getPlatformVersion()
{
$ua = $_SERVER['HTTP_USER_AGENT'];
if (strpos($ua, 'Android') !== false) {
preg_match("/(?<=Android)[\d\.]{1,}/", $ua, $version);
return $version[0];
} elseif (strpos($ua, 'iPhone') !== false) {
preg_match("/(?<=CPU iPhone OS)[\d\_]{1,}/", $ua, $version);
return str_replace('_', '.', $version[0]);
} elseif (strpos($ua, 'iPad') !== false) {
preg_match("/(?<=CPU OS)[\d\_]{1,}/", $ua, $version);
return str_replace('_', '.', $version[0]);
}
}
/**
* 获取设备的品牌
* @return string
*/
public function getDeviceModel()
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
return $user_agent;
}
/**
* 获取设备的品牌
* @return string
*/
public function getDeviceBrand()
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (stripos($user_agent, "iPhone")!==false) {
$brand = 'iPhone';
} elseif (stripos($user_agent, "SAMSUNG")!==false || stripos($user_agent, "Galaxy")!==false || strpos($user_agent, "GT-")!==false || strpos($user_agent, "SCH-")!==false || strpos($user_agent, "SM-")!==false) {
$brand = '三星';
} elseif (stripos($user_agent, "Huawei")!==false || stripos($user_agent, "Honor")!==false || stripos($user_agent, "H60-")!==false || stripos($user_agent, "H30-")!==false) {
$brand = '华为';
} elseif (stripos($user_agent, "Lenovo")!==false) {
$brand = '联想';
} elseif (strpos($user_agent, "MI-ONE")!==false || strpos($user_agent, "MI 1S")!==false || strpos($user_agent, "MI 2")!==false || strpos($user_agent, "MI 3")!==false || strpos($user_agent, "MI 4")!==false || strpos($user_agent, "MI-4")!==false) {
$brand = '小米';
} elseif (strpos($user_agent, "HM NOTE")!==false || strpos($user_agent, "HM201")!==false) {
$brand = '红米';
} elseif (stripos($user_agent, "Coolpad")!==false || strpos($user_agent, "8190Q")!==false || strpos($user_agent, "5910")!==false) {
$brand = '酷派';
} elseif (stripos($user_agent, "ZTE")!==false || stripos($user_agent, "X9180")!==false || stripos($user_agent, "N9180")!==false || stripos($user_agent, "U9180")!==false) {
$brand = '中兴';
} elseif (stripos($user_agent, "OPPO")!==false || strpos($user_agent, "X9007")!==false || strpos($user_agent, "X907")!==false || strpos($user_agent, "X909")!==false || strpos($user_agent, "R831S")!==false || strpos($user_agent, "R827T")!==false || strpos($user_agent, "R821T")!==false || strpos($user_agent, "R811")!==false || strpos($user_agent, "R2017")!==false) {
$brand = 'OPPO';
} elseif (strpos($user_agent, "HTC")!==false || stripos($user_agent, "Desire")!==false) {
$brand = 'HTC';
} elseif (stripos($user_agent, "vivo")!==false) {
$brand = 'vivo';
} elseif (stripos($user_agent, "K-Touch")!==false) {
$brand = '天语';
} elseif (stripos($user_agent, "Nubia")!==false || stripos($user_agent, "NX50")!==false || stripos($user_agent, "NX40")!==false) {
$brand = '努比亚';
} elseif (strpos($user_agent, "M045")!==false || strpos($user_agent, "M032")!==false || strpos($user_agent, "M355")!==false) {
$brand = '魅族';
} elseif (stripos($user_agent, "DOOV")!==false) {
$brand = '朵唯';
} elseif (stripos($user_agent, "GFIVE")!==false) {
$brand = '基伍';
} elseif (stripos($user_agent, "Gionee")!==false || strpos($user_agent, "GN")!==false) {
$brand = '金立';
} elseif (stripos($user_agent, "HS-U")!==false || stripos($user_agent, "HS-E")!==false) {
$brand = '海信';
} elseif (stripos($user_agent, "Nokia")!==false) {
$brand = '诺基亚';
} else {
$brand = '其他手机';
}
return $brand;
}
}