PHP获取浏览器,PHP获取IP地址,PHP获取设备型号

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php
 
//获取浏览器
 
function get_broswer(){
    $sys = $_SERVER['HTTP_USER_AGENT'];  //获取用户代理字符串
 
    $exp[0] = "未知浏览器";
    $exp[1] = "";
  //stripos() 函数查找字符串在另一字符串中第一次出现的位置(不区分大小写)    preg_match()执行匹配正则表达式
    if (stripos($sys, "Firefox/") > 0) {
         preg_match("/Firefox\/([^;)]+)+/i", $sys, $b);
         $exp[0] = "Firefox";
         $exp[1] = $b[1];  //获取火狐浏览器的版本号
    if (stripos($sys, "Maxthon") > 0) {
         preg_match("/Maxthon\/([\d\.]+)/", $sys, $aoyou);
         $exp[0] = "傲游";
         $exp[1] = $aoyou[1];
    if (stripos($sys, "MSIE") > 0) {
         preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie);
         $exp[0] = "IE";
         $exp[1] = $ie[1];  //获取IE的版本号
    if (stripos($sys, "OPR") > 0) {
             preg_match("/OPR\/([\d\.]+)/", $sys, $opera);
         $exp[0] = "Opera";
         $exp[1] = $opera[1];
    if(stripos($sys, "Edge") > 0) {
         //win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配
         preg_match("/Edge\/([\d\.]+)/", $sys, $Edge);
         $exp[0] = "Edge";
         $exp[1] = $Edge[1];
    if (stripos($sys, "Chrome") > 0) {
             preg_match("/Chrome\/([\d\.]+)/", $sys, $google);
         $exp[0] = "Chrome";
         $exp[1] = $google[1];  //获取google chrome的版本号
    if(stripos($sys,'rv:')>0 && stripos($sys,'Gecko')>0){
         preg_match("/rv:([\d\.]+)/", $sys, $IE);
             $exp[0] = "IE";
         $exp[1] = $IE[1];
    }
    return $exp[0].'('.$exp[1].')';
}
 
 
 
 
 
//获取操作系统
function get_os(){
    $agent = $_SERVER['HTTP_USER_AGENT'];
    $os = '未知操作系统';
 
    if (preg_match('/win/i', $agent) && strpos($agent, '95'))
    {
      $os = 'Windows 95';
    }
      if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90'))
    {
      $os = 'Windows ME';
    }
      if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent))
    {
      $os = 'Windows 98';
    }
    if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent))
    {
      $os = 'Windows NT';
    }
    if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent))
    {
      $os = 'Windows Vista';
    }
    if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent))
    {
      $os = 'Windows 7';
    }
    if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent))
    {
      $os = 'Windows 8';
    }
    if(preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent))
    {
      $os = 'Windows 10';#添加win10判断
    }
    if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent))
    {
      $os = 'Windows XP';
    }
    if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent))
    {
      $os = 'Windows 2000';
    }
 
    if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent))
    {
      $os = 'Windows 32';
    }
    if (preg_match('/linux/i', $agent))
    {
      $os = 'Linux';
    }
    if (preg_match('/unix/i', $agent))
    {
      $os = 'Unix';
    }
    if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent))
    {
      $os = 'SunOS';
    }
      if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent))
    {
      $os = 'IBM OS/2';
    }
    if (preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent))
    {
      $os = 'Macintosh';
    }
    if (preg_match('/PowerPC/i', $agent))
    {
      $os = 'PowerPC';
    }
    if (preg_match('/AIX/i', $agent))
    {
      $os = 'AIX';
    }
    if (preg_match('/HPUX/i', $agent))
    {
      $os = 'HPUX';
    }
    if (preg_match('/NetBSD/i', $agent))
    {
      $os = 'NetBSD';
    }
    if (preg_match('/BSD/i', $agent))
    {
      $os = 'BSD';
    }
    if (preg_match('/OSF1/i', $agent))
    {
      $os = 'OSF1';
    }
    if (preg_match('/IRIX/i', $agent))
    {
      $os = 'IRIX';
    }
    if (preg_match('/FreeBSD/i', $agent))
    {
      $os = 'FreeBSD';
    }
    if (preg_match('/teleport/i', $agent))
    {
      $os = 'teleport';
    }
    if (preg_match('/flashget/i', $agent))
    {
      $os = 'flashget';
    }
      if (preg_match('/webzip/i', $agent))
    {
      $os = 'webzip';
    }
      if (preg_match('/offline/i', $agent))
    {
      $os = 'offline';
    }
     if(strpos($agent, 'iphone')){
      $os = 'iphone';
    }
     if(strpos($agent, 'ipad')){
      $os = 'ipad';
    }
     if(strpos($agent, 'android')){
      $os = 'android';
    }
    if (stripos($agent, "SAMSUNG")!==false || stripos($agent, "Galaxy")!==false || strpos($agent, "GT-")!==false || strpos($agent, "SCH-")!==false || strpos($agent, "SM-")!==false) {
        $os = 'android ->三星';
    }
    if (stripos($agent, "Huawei")!==false || stripos($agent, "Honor")!==false || stripos($agent, "H60-")!==false || stripos($agent, "H30-")!==false) {
        $os = 'android ->华为';
    }
    if (stripos($agent, "Lenovo")!==false) {
        $os = 'android ->联想';
    }
    if (strpos($agent, "MI-ONE")!==false || strpos($agent, "MI 1S")!==false || strpos($agent, "MI 2")!==false || strpos($agent, "MI 3")!==false || strpos($agent, "MI 4")!==false || strpos($agent, "MI-4")!==false) {
        $os = 'android ->小米';
    }
    if (strpos($agent, "HM NOTE")!==false || strpos($agent, "HM201")!==false) {
        $os = 'android ->红米';
    }
    if (stripos($agent, "Coolpad")!==false || strpos($agent, "8190Q")!==false || strpos($agent, "5910")!==false) {
        $os = 'android ->酷派';
    }
    if (stripos($agent, "ZTE")!==false || stripos($agent, "X9180")!==false || stripos($agent, "N9180")!==false || stripos($agent, "U9180")!==false) {
        $os = 'android ->中兴';
    }
    if (stripos($agent, "OPPO")!==false || strpos($agent, "X9007")!==false || strpos($agent, "X907")!==false || strpos($agent, "X909")!==false || strpos($agent, "R831S")!==false || strpos($agent, "R827T")!==false || strpos($agent, "R821T")!==false || strpos($agent, "R811")!==false || strpos($agent, "R2017")!==false) {
        $os = 'android ->OPPO';
    }
    if (strpos($agent, "HTC")!==false || stripos($agent, "Desire")!==false) {
        $os = 'android ->HTC';
    }
    if (stripos($agent, "vivo")!==false) {
        $os = 'android ->vivo';
    }
    if (stripos($agent, "K-Touch")!==false) {
        $os = 'android ->天语';
    }
    if (stripos($agent, "Nubia")!==false || stripos($agent, "NX50")!==false || stripos($agent, "NX40")!==false) {
        $os = 'android ->努比亚';
    }
    if (strpos($agent, "M045")!==false || strpos($agent, "M032")!==false || strpos($agent, "M355")!==false) {
        $os = 'android ->魅族';
    }
    if (stripos($agent, "DOOV")!==false) {
        $os = 'android ->朵唯';
    }
    if (stripos($agent, "GFIVE")!==false) {
        $os = 'android ->基伍';
    }
    if (stripos($agent, "Gionee")!==false || strpos($agent, "GN")!==false) {
        $os = 'android ->金立';
    }
    if (stripos($agent, "HS-U")!==false || stripos($agent, "HS-E")!==false) {
        $os = 'android ->海信';
    }
    if (stripos($agent, "Nokia")!==false) {
        $os = 'android ->诺基亚';
    }
    return $os;
}
 
 
 
/**
 * 是否移动端访问访问
 * 判断当前访问的用户是  PC端  还是 手机端  返回true 为手机端  false 为PC 端
 * @return boolean
 */
function isMobile(){
        $code = 2;
        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
        if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
        {
            $code = 1;
        }
        // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
        if (isset ($_SERVER['HTTP_VIA']))
        {
            // 找不到为flase,否则为true
            $code = stristr($_SERVER['HTTP_VIA'], "wap") ? 1 : 2;
        }
        // 脑残法,判断手机发送的客户端标志,兼容性有待提高
        if (isset ($_SERVER['HTTP_USER_AGENT']))
        {
            $clientkeywords = array ('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',
                'mobile'
                );
            // 从HTTP_USER_AGENT中查找手机浏览器的关键字
            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
            {
                $code = 1;
            }
        }
        // 协议法,因为有可能不准确,放到最后判断
        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'))))
            {
                $code = 1;
            }
        }
        return $code;
}
 
 
/**
 * 获取客户端IP地址
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
 * @return string
 */
function get_client_ip()
{
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
        $ip = getenv("HTTP_CLIENT_IP");
      if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
      if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
        $ip = getenv("REMOTE_ADDR");
      if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
        $ip = $_SERVER['REMOTE_ADDR'];
 
       if(empty($ip))$ip = "unknown";
    return ($ip);
}
//获得访客真实ip
   function Getip()
 
  {
 
    if (! empty($_SERVER["HTTP_CLIENT_IP"])) {
 
      $ip = $_SERVER["HTTP_CLIENT_IP"];
 
    }
 
    if (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { // 获取代理ip
 
      $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
 
    }
 
    if ($ip) {
 
      $ips = array_unshift($ips, $ip);
 
    }
 
    $count = count($ips);
 
    for ($i = 0; $i < $count; $i ++) {
 
      if (! preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i])) { // 排除局域网ip
 
        $ip = $ips[$i];
 
        break;
 
      }
 
    }
 
    $tip = empty($_SERVER['REMOTE_ADDR']) ? $ip : $_SERVER['REMOTE_ADDR'];
 
    if ($tip == "127.0.0.1") { // 获得本地真实IP
 
      return getLocalIP();
 
    } else {
 
      return $tip;
 
    }
 
  }
  
 //获取本地真实IP
function getLocalIP() {
    $preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";
//获取操作系统为win2000/xp、win7的本机IP真实地址
    exec("ipconfig", $out, $stats);
    if (!empty($out)) {
        foreach ($out AS $row) {
            if (strstr($row, "IP") && strstr($row, ":") && !strstr($row, "IPv6")) {
                $tmpIp = explode(":", $row);
                if (preg_match($preg, trim($tmpIp[1]))) {
                    return trim($tmpIp[1]);
                }
            }
        }
    }
//获取操作系统为linux类型的本机IP真实地址
    exec("ifconfig", $out, $stats);
    if (!empty($out)) {
        if (isset($out[1]) && strstr($out[1], 'addr:')) {
            $tmpArray = explode(":", $out[1]);
            $tmpIp = explode(" ", $tmpArray[1]);
            if (preg_match($preg, trim($tmpIp[0]))) {
                return trim($tmpIp[0]);
            }
        }
    }
    return '127.0.0.1';
}
 
   // //根据ip获得访客所在地地名
  function Getaddress($ip = '')
  {
    if (empty($ip)) {
      $ip = $this->Getip();
    }
    $ipadd = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" . $ip); // 根据新浪api接口获取
    if ($ipadd) {
      $charset = iconv("gbk", "utf-8", $ipadd);
      preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $charset, $ipadds);
      return $ipadds; // 返回一个二维数组
    } else {
      return "addree is none";
    }
  }

  

posted @   minch  阅读(170)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示