PHP: Browser, Operating System (OS), Device, and Language Detect

https://github.com/sinergi/php-browser-detector

Device.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
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="author" content="geovindu" />
  <title>php 学习测试客户端环境</title>
    <meta name="keywords" content="geovindu">
<meta name="description" content="涂聚文">
    <meta
    </head>
<body>   
<?php
// win10 环境下用各浏览器检测的数据:
//谷歌Chrome  Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36
//微软Edge  Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134
//微软IE Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
//火狐Firefox  Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0
//欧朋Opera  Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.170 Safari/537.36 OPR/53.0.2907.68
//苹果Safari  Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2
     
include('user_agent.php');
$geovindu="hi,how are you";
$number=2000;
$html="<br/>";
$word=$html.$geovindu.$number;
echo "geovindu<br/>";
echo "涂聚文";
echo($word);
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$lug=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
$ua = new CI_User_agent($user_agent);
$Duip=$_SERVER['SERVER_ADDR'];
$geovindu=new RemoteAddress();
     
 
//获取浏览器
function getBrowse()
{
    global $_SERVER;
    $Agent = $_SERVER['HTTP_USER_AGENT'];
    $browseinfo='';
    if(ereg('Mozilla', $Agent) && !ereg('MSIE', $Agent)){
        $browseinfo = 'Netscape Navigator';
    }
    if(ereg('Opera', $Agent)) {
        $browseinfo = 'Opera';
    }
    if(ereg('Mozilla', $Agent) && ereg('MSIE', $Agent)){
 
        $browseinfo = 'Internet Explorer';
    }
    if(ereg('Chrome', $Agent)){
        $browseinfo="Chrome";
    }
    if(ereg('Safari', $Agent)){
        $browseinfo="Safari";
    }
    if(ereg('Firefox', $Agent)){
        $browseinfo="Firefox";
    }
 
    return $browseinfo;
}
//获取ip
function getIP()
{
    global $_SERVER;
    /* //方法1
    if (getenv('HTTP_CLIENT_IP')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } else if (getenv('HTTP_X_FORWARDED_FOR')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    } else if (getenv('REMOTE_ADDR')) {
        $ip = getenv('REMOTE_ADDR');
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }*/
    //方法2
    /*
    error_reporting (E_ERROR | E_WARNING | E_PARSE);
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";
}
*/
    //$ip = $_SERVER["REMOTE_ADDR"];
    //方法
    /*
    $ip = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$ip = ($ip) ? $ip : $_SERVER["REMOTE_ADDR"];
*/
     
//方法   
/*
foreach (array('HTTP_CLIENT_IP',
                   'HTTP_X_FORWARDED_FOR',
                   'HTTP_X_FORWARDED',
                   'HTTP_X_CLUSTER_CLIENT_IP',
                   'HTTP_FORWARDED_FOR',
                   'HTTP_FORWARDED',
                   'REMOTE_ADDR') as $key){
        if (array_key_exists($key, $_SERVER) === true){
            foreach (explode(',', $_SERVER[$key]) as $ip){
                $ip = trim($ip); // Just to be safe
 
                if (filter_var($ip,
                               FILTER_VALIDATE_IP,
                               FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
                    !== false) {
 
                    //return $ip;
                }
            }
        }
    }
                     if(empty($ip))
                    {
                         $ip='127.0.0.1';
                     }
                    if($ip='::1')
                    {
                        $ip='127.0.0.1';
                    }
                    */
    /*
     if ($_SERVER['HTTP_CLIENT_IP'] != '::1')
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    else if ($_SERVER['HTTP_X_FORWARDED_FOR'] != '::1')
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if ($_SERVER['HTTP_X_FORWARDED'] != '::1')
        $ip = $_SERVER['HTTP_X_FORWARDED'];
    else if ($_SERVER['HTTP_FORWARDED_FOR'] != '::1')
        $ip = $_SERVER['HTTP_FORWARDED_FOR'];
    else if ($_SERVER['HTTP_FORWARDED'] != '::1')
        $ip = $_SERVER['HTTP_FORWARDED'];
    else if ($_SERVER['REMOTE_ADDR'] != '::1')
        $ip = $_SERVER['REMOTE_ADDR'];
    else
        $ip = '127.0.0.1';
    */
        // check for shared internet/ISP IP
    if (!empty($_SERVER['HTTP_CLIENT_IP']) && validate_ip($_SERVER['HTTP_CLIENT_IP'])) {
        return $_SERVER['HTTP_CLIENT_IP'];
    }
 
    // check for IPs passing through proxies
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        // check if multiple ips exist in var
        if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false) {
            $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            foreach ($iplist as $ip) {
                if (validate_ip($ip))
                    return $ip;
            }
        } else {
            if (validate_ip($_SERVER['HTTP_X_FORWARDED_FOR']))
                return $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED']) && validate_ip($_SERVER['HTTP_X_FORWARDED']))
        return $_SERVER['HTTP_X_FORWARDED'];
    if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
        return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
    if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
        return $_SERVER['HTTP_FORWARDED_FOR'];
    if (!empty($_SERVER['HTTP_FORWARDED']) && validate_ip($_SERVER['HTTP_FORWARDED']))
        return $_SERVER['HTTP_FORWARDED'];
    $ip=$_SERVER['REMOTE_ADDR'];
    $ip="::1"?"127.0.0.1":$ip;
    return $ip;
}
//获取用户系统
function getOS()
{
    global $_SERVER;
    $agent = $_SERVER['HTTP_USER_AGENT'];
    $os = false;
    if (eregi('win', $agent) && strpos($agent, '95')){
        $os = 'Windows 95';
    }elseif (eregi('win 9x', $agent) && strpos($agent, '4.90')){
        $os = 'Windows ME';
    }elseif (eregi('win', $agent) && ereg('98', $agent)){
        $os = 'Windows 98';
    }elseif (eregi('win', $agent) && eregi('nt 5.1', $agent)){
        $os = 'Windows XP';
    }elseif (eregi('win', $agent) && eregi('nt 5.2', $agent)){   
        $os = 'Windows 2003';
    }elseif (eregi('win', $agent) && eregi('nt 5', $agent)){
        $os = 'Windows 2000';
    }elseif (eregi('win', $agent) && eregi('nt', $agent)){
        $os = 'Windows NT';
    }elseif (eregi('win', $agent) && ereg('32', $agent)){
        $os = 'Windows 32';
    }elseif (eregi('linux', $agent)){
        $os = 'Linux';
    }elseif (eregi('unix', $agent)){
        $os = 'Unix';
    }elseif (eregi('sun', $agent) && eregi('os', $agent)){
        $os = 'SunOS';
    }elseif (eregi('ibm', $agent) && eregi('os', $agent)){
        $os = 'IBM OS/2';
    }elseif (eregi('Mac', $agent) && eregi('PC', $agent)){
        $os = 'Macintosh';
    }elseif (eregi('PowerPC', $agent)){
        $os = 'PowerPC';
    }elseif (eregi('AIX', $agent)){
        $os = 'AIX';
    }elseif (eregi('HPUX', $agent)){
        $os = 'HPUX';
    }elseif (eregi('NetBSD', $agent)){
        $os = 'NetBSD';
    }elseif (eregi('BSD', $agent)){
        $os = 'BSD';
    }elseif (ereg('OSF1', $agent)){
        $os = 'OSF1';
    }elseif (ereg('IRIX', $agent)){
        $os = 'IRIX';
    }elseif (eregi('FreeBSD', $agent)){
        $os = 'FreeBSD';
    }elseif (eregi('teleport', $agent)){
        $os = 'teleport';
    }elseif (eregi('flashget', $agent)){
        $os = 'flashget';
    }elseif (eregi('webzip', $agent)){
        $os = 'webzip';
    }elseif (eregi('offline', $agent)){
        $os = 'offline';
    }else{
        $os = 'Unknown';
    }
    return $os;
}
phpinfo();  //检测PHP环境
?>
操作系统:<input type="text" value="<?php echo  $ua->platform() ?>" id="textos"/><br/>
浏览器:    <input type="text" value="<?php echo  $ua->browser() ?>" id="textbrowser"/><br/>
浏览器版本   <input type="text" value="<?php echo  $ua->version() ?>" id="textversion"/><br/>
    <input type="text" value="<?php echo  $ua->robot() ?>" id="textrobot"/><br/>
手机品牌:<input type="text" value="<?php echo  $ua->mobile() ?>" id="textmobile"/><br/>
客户环境检测:<input type="text" value="<?php echo  $user_agent ?>" id="textgetusert" size="100"/><br/>
浏览器语言:<input type="text" value="<?php echo  $lug ?>" id="textlg"/><br/>
    <input type="text" value="<?php echo getIP() ?>" id="textip"/><br/>
    <?php echo $Duip."<br/>" ?>
    <?php echo $geovindu->getIpAddress() ?>
     
</body>
    </html>

  

 

user_agent.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
<?php
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package     CodeIgniter
 * @author      ExpressionEngine Dev Team
 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license     http://codeigniter.com/user_guide/license.html
 * @link        http://codeigniter.com
 * @since       Version 1.0
 * @filesource
 */
 
// ------------------------------------------------------------------------
 
/**
 * User Agent Class
 *
 * Identifies the platform, browser, robot, or mobile devise of the browsing agent
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @category    User Agent
 * @author      ExpressionEngine Dev Team
 * @link        http://codeigniter.com/user_guide/libraries/user_agent.html
   参考: https://en.wikipedia.org/wiki/Windows_NT  geovindu 涂聚文 edit update 20180712
 */
class CI_User_agent {
 
    var $platforms = array (
                    'windows nt 10.0'   => 'Win10',
                    'windows nt 6.3'    => 'Windows 8.1 Windows Server 2012 R2',
                    'windows nt 6.2'    => 'Win8',
                    'windows nt 6.1'    => 'Win7',
                    'windows nt 6.0'    => 'Win Longhorn',
                    'windows nt 5.2'    => 'Win2003',
                    'windows nt 5.0'    => 'Win2000',
                    'windows nt 5.1'    => 'WinXP',
                    'windows nt 4.0'    => 'Windows NT 4.0',
                    'winnt4.0'          => 'Windows NT 4.0',
                    'winnt 4.0'         => 'Windows NT',
                    'winnt'             => 'Windows NT',
                    'windows 98'        => 'Win98',
                    'win98'             => 'Win98',
                    'windows 95'        => 'Win95',
                    'win95'             => 'Win95',
                    'windows'           => 'Unknown Windows OS',
                    'os x'              => 'MacOS X',
                    'ppc mac'           => 'Power PC Mac',
                    'freebsd'           => 'FreeBSD',
                    'ppc'               => 'Macintosh',
                    'linux'             => 'Linux',
                    'debian'            => 'Debian',
                    'sunos'             => 'Sun Solaris',
                    'beos'              => 'BeOS',
                    'apachebench'       => 'ApacheBench',
                    'aix'               => 'AIX',
                    'irix'              => 'Irix',
                    'osf'               => 'DEC OSF',
                    'hp-ux'             => 'HP-UX',
                    'netbsd'            => 'NetBSD',
                    'bsdi'              => 'BSDi',
                    'openbsd'           => 'OpenBSD',
                    'gnu'               => 'GNU/Linux',
                    'unix'              => 'Unknown Unix OS'
                );
 
 
    // The order of this array should NOT be changed. Many browsers return
    // multiple browser types so we want to identify the sub-type first.
    var $browsers = array(
                    'Flock'             => 'Flock',
                    'Chrome'            => 'Chrome',
                    'Opera'             => 'Opera',
                    'MSIE'              => 'IE',
                    'Internet Explorer' => 'IE',
                    'Shiira'            => 'Shiira',
                    'Firefox'           => 'Firefox',
                    'Chimera'           => 'Chimera',
                    'Phoenix'           => 'Phoenix',
                    'Firebird'          => 'Firebird',
                    'Camino'            => 'Camino',
                    'Netscape'          => 'Netscape',
                    'OmniWeb'           => 'OmniWeb',
                    'Safari'            => 'Safari',
                    'Mozilla'           => 'Mozilla',
                    'Konqueror'         => 'Konqueror',
                    'icab'              => 'iCab',
                    'Lynx'              => 'Lynx',
                    'Links'             => 'Links',
                    'hotjava'           => 'HotJava',
                    'amaya'             => 'Amaya',
                    'IBrowse'           => 'IBrowse'
                );
 
    var $mobiles = array(
                    // legacy array, old values commented out
                    'mobileexplorer'    => 'Mobile Explorer',
    //                  'openwave'          => 'Open Wave',
    //                  'opera mini'        => 'Opera Mini',
    //                  'operamini'         => 'Opera Mini',
    //                  'elaine'            => 'Palm',
                    'palmsource'        => 'Palm',
    //                  'digital paths'     => 'Palm',
    //                  'avantgo'           => 'Avantgo',
    //                  'xiino'             => 'Xiino',
                    'palmscape'         => 'Palmscape',
    //                  'nokia'             => 'Nokia',
    //                  'ericsson'          => 'Ericsson',
    //                  'blackberry'        => 'BlackBerry',
    //                  'motorola'          => 'Motorola'
 
                    // Phones and Manufacturers
                    'motorola'          => "Motorola",
                    'nokia'             => "Nokia",
                    'palm'              => "Palm",
                    'iphone'            => "Apple iPhone",
                    'ipad'              => "iPad",
                    'ipod'              => "Apple iPod Touch",
                    'sony'              => "Sony Ericsson",
                    'ericsson'          => "Sony Ericsson",
                    'blackberry'        => "BlackBerry",
                    'cocoon'            => "O2 Cocoon",
                    'blazer'            => "Treo",
                    'lg'                => "LG",
                    'amoi'              => "Amoi",
                    'xda'               => "XDA",
                    'mda'               => "MDA",
                    'vario'             => "Vario",
                    'htc'               => "HTC",
                    'samsung'           => "Samsung",
                    'sharp'             => "Sharp",
                    'sie-'              => "Siemens",
                    'alcatel'           => "Alcatel",
                    'benq'              => "BenQ",
                    'ipaq'              => "HP iPaq",
                    'mot-'              => "Motorola",
                    'playstation portable'  => "PlayStation Portable",
                    'hiptop'            => "Danger Hiptop",
                    'nec-'              => "NEC",
                    'panasonic'         => "Panasonic",
                    'philips'           => "Philips",
                    'sagem'             => "Sagem",
                    'sanyo'             => "Sanyo",
                    'spv'               => "SPV",
                    'zte'               => "ZTE",
                    'sendo'             => "Sendo",
                    'huawei'            => "Huawei",
                    'vivo'              => "Vivo",
                    'oppo'              => "OPPO",
                    // Operating Systems
                    'symbian'               => "Symbian",
                    'SymbianOS'             => "SymbianOS",
                    'elaine'                => "Palm",
                    'palm'                  => "Palm",
                    'series60'              => "Symbian S60",
                    'windows ce'            => "Windows CE",
 
                    // Browsers
                    'obigo'                 => "Obigo",
                    'netfront'              => "Netfront Browser",
                    'openwave'              => "Openwave Browser",
                    'mobilexplorer'         => "Mobile Explorer",
                    'operamini'             => "Opera Mini",
                    'opera mini'            => "Opera Mini",
 
                    // Other
                    'digital paths'         => "Digital Paths",
                    'avantgo'               => "AvantGo",
                    'xiino'                 => "Xiino",
                    'novarra'               => "Novarra Transcoder",
                    'vodafone'              => "Vodafone",
                    'docomo'                => "NTT DoCoMo",
                    'o2'                    => "O2",
 
                    // Fallback
                    'mobile'                => "Generic Mobile",
                    'wireless'              => "Generic Mobile",
                    'j2me'                  => "Generic Mobile",
                    'midp'                  => "Generic Mobile",
                    'cldc'                  => "Generic Mobile",
                    'up.link'               => "Generic Mobile",
                    'up.browser'            => "Generic Mobile",
                    'smartphone'            => "Generic Mobile",
                    'cellphone'             => "Generic Mobile"
                );
 
    // There are hundreds of bots but these are the most common.
    var $robots = array(
                    'googlebot'         => 'Googlebot',
                    'msnbot'            => 'MSNBot',
                    'slurp'             => 'Inktomi Slurp',
                    'yahoo'             => 'Yahoo',
                    'askjeeves'         => 'AskJeeves',
                    'fastcrawler'       => 'FastCrawler',
                    'infoseek'          => 'InfoSeek Robot 1.0',
                    'lycos'             => 'Lycos'
                );
 
 
    var $agent      = NULL;
 
    var $is_browser = FALSE;
    var $is_robot   = FALSE;
    var $is_mobile  = FALSE;
 
    var $languages  = array();
    var $charsets   = array();
 
    var $platform   = '';
    var $browser    = '';
    var $version    = '';
    var $mobile     = '';
    var $robot      = '';
 
    /**
     * Constructor
     *
     * Sets the User Agent and runs the compilation routine
     *
     * @access  public
     * @return  void
     */
    public function __construct($agent = '')
    {
        if ($agent != '')
        {
            $this->agent = $agent;
        }
        else if (isset($_SERVER['HTTP_USER_AGENT']))
        {
            $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
        }
 
        if ( ! is_null($this->agent))
        {
            $this->_compile_data();
        }
 
        //log_message('debug', "User Agent Class Initialized");
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Compile the User Agent Data
     *
     * @access  private
     * @return  bool
     */
    private function _compile_data()
    {
        $this->_set_platform();
 
        foreach (array('_set_robot', '_set_browser', '_set_mobile') as $function)
        {
            if ($this->$function() === TRUE)
            {
                break;
            }
        }
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Set the Platform
     *
     * @access  private
     * @return  mixed
     */
    private function _set_platform()
    {
        if (is_array($this->platforms) AND count($this->platforms) > 0)
        {
            foreach ($this->platforms as $key => $val)
            {
                if (preg_match("|".preg_quote($key)."|i", $this->agent))
                {
                    $this->platform = $val;
                    return TRUE;
                }
            }
        }
        $this->platform = 'Unknown Platform';
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Set the Browser
     *
     * @access  private
     * @return  bool
     */
    private function _set_browser()
    {
        if (is_array($this->browsers) AND count($this->browsers) > 0)
        {
            foreach ($this->browsers as $key => $val)
            {
                if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match))
                {
                    $this->is_browser = TRUE;
                    $this->version = $match[1];
                    $this->browser = $val;
                    $this->_set_mobile();
                    return TRUE;
                }
            }
        }
        return FALSE;
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Set the Robot
     *
     * @access  private
     * @return  bool
     */
    private function _set_robot()
    {
        if (is_array($this->robots) AND count($this->robots) > 0)
        {
            foreach ($this->robots as $key => $val)
            {
                if (preg_match("|".preg_quote($key)."|i", $this->agent))
                {
                    $this->is_robot = TRUE;
                    $this->robot = $val;
                    return TRUE;
                }
            }
        }
        return FALSE;
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Set the Mobile Device
     *
     * @access  private
     * @return  bool
     */
    private function _set_mobile()
    {
        if (is_array($this->mobiles) AND count($this->mobiles) > 0)
        {
            foreach ($this->mobiles as $key => $val)
            {
                if (FALSE !== (strpos(strtolower($this->agent), $key)))
                {
                    $this->is_mobile = TRUE;
                    $this->mobile = $val;
                    return TRUE;
                }
            }
        }
        return FALSE;
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Set the accepted languages
     *
     * @access  private
     * @return  void
     */
    private function _set_languages()
    {
        if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
        {
            $languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
 
            $this->languages = explode(',', $languages);
        }
 
        if (count($this->languages) == 0)
        {
            $this->languages = array('Undefined');
        }
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Set the accepted character sets
     *
     * @access  private
     * @return  void
     */
    private function _set_charsets()
    {
        if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
        {
            $charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])));
 
            $this->charsets = explode(',', $charsets);
        }
 
        if (count($this->charsets) == 0)
        {
            $this->charsets = array('Undefined');
        }
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Is Browser
     *
     * @access  public
     * @return  bool
     */
    public function is_browser($key = NULL)
    {
        if ( ! $this->is_browser)
        {
            return FALSE;
        }
 
        // No need to be specific, it's a browser
        if ($key === NULL)
        {
            return TRUE;
        }
 
        // Check for a specific browser
        return array_key_exists($key, $this->browsers) AND $this->browser === $this->browsers[$key];
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Is Robot
     *
     * @access  public
     * @return  bool
     */
    public function is_robot($key = NULL)
    {
        if ( ! $this->is_robot)
        {
            return FALSE;
        }
 
        // No need to be specific, it's a robot
        if ($key === NULL)
        {
            return TRUE;
        }
 
        // Check for a specific robot
        return array_key_exists($key, $this->robots) AND $this->robot === $this->robots[$key];
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Is Mobile
     *
     * @access  public
     * @return  bool
     */
    public function is_mobile($key = NULL)
    {
        if ( ! $this->is_mobile)
        {
            return FALSE;
        }
 
        // No need to be specific, it's a mobile
        if ($key === NULL)
        {
            return TRUE;
        }
 
        // Check for a specific robot
        return array_key_exists($key, $this->mobiles) AND $this->mobile === $this->mobiles[$key];
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Is this a referral from another site?
     *
     * @access  public
     * @return  bool
     */
    public function is_referral()
    {
        if ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '')
        {
            return FALSE;
        }
        return TRUE;
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Agent String
     *
     * @access  public
     * @return  string
     */
    public function agent_string()
    {
        return $this->agent;
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Get Platform
     *
     * @access  public
     * @return  string
     */
    public function platform()
    {
        return $this->platform;
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Get Browser Name
     *
     * @access  public
     * @return  string
     */
    public function browser()
    {
        return $this->browser;
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Get the Browser Version
     *
     * @access  public
     * @return  string
     */
    public function version()
    {
        return $this->version;
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Get The Robot Name
     *
     * @access  public
     * @return  string
     */
    public function robot()
    {
        return $this->robot;
    }
    // --------------------------------------------------------------------
 
    /**
     * Get the Mobile Device
     *
     * @access  public
     * @return  string
     */
    public function mobile()
    {
        return $this->mobile;
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Get the referrer
     *
     * @access  public
     * @return  bool
     */
    public function referrer()
    {
        return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Get the accepted languages
     *
     * @access  public
     * @return  array
     */
    public function languages()
    {
        if (count($this->languages) == 0)
        {
            $this->_set_languages();
        }
 
        return $this->languages;
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Get the accepted Character Sets
     *
     * @access  public
     * @return  array
     */
    public function charsets()
    {
        if (count($this->charsets) == 0)
        {
            $this->_set_charsets();
        }
 
        return $this->charsets;
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Test for a particular language
     *
     * @access  public
     * @return  bool
     */
    public function accept_lang($lang = 'en')
    {
        return (in_array(strtolower($lang), $this->languages(), TRUE));
    }
 
    // --------------------------------------------------------------------
 
    /**
     * Test for a particular character set
     *
     * @access  public
     * @return  bool
     */
    public function accept_charset($charset = 'utf-8')
    {
        return (in_array(strtolower($charset), $this->charsets(), TRUE));
    }
 
}
 
// 获取IP
//https://raw.githubusercontent.com/zendframework/zend-http/master/src/PhpEnvironment/RemoteAddress.php
//http://php.net/manual/en/reserved.variables.server.php
 
/**
 * Functionality for determining client IP address.
 */
class RemoteAddress
{
    /**
     * Whether to use proxy addresses or not.
     *
     * As default this setting is disabled - IP address is mostly needed to increase
     * security. HTTP_* are not reliable since can easily be spoofed. It can be enabled
     * just for more flexibility, but if user uses proxy to connect to trusted services
     * it's his/her own risk, only reliable field for IP address is $_SERVER['REMOTE_ADDR'].
     *
     * @var bool
     */
    protected $useProxy = false;
 
    /**
     * List of trusted proxy IP addresses
     *
     * @var array
     */
    protected $trustedProxies = [];
 
    /**
     * HTTP header to introspect for proxies
     *
     * @var string
     */
    protected $proxyHeader = 'HTTP_X_FORWARDED_FOR';
 
    /**
     * Changes proxy handling setting.
     *
     * This must be static method, since validators are recovered automatically
     * at session read, so this is the only way to switch setting.
     *
     * @param  bool  $useProxy Whether to check also proxied IP addresses.
     * @return RemoteAddress
     */
    public function setUseProxy($useProxy = true)
    {
        $this->useProxy = $useProxy;
        return $this;
    }
 
    /**
     * Checks proxy handling setting.
     *
     * @return bool Current setting value.
     */
    public function getUseProxy()
    {
        return $this->useProxy;
    }
 
    /**
     * Set list of trusted proxy addresses
     *
     * @param  array $trustedProxies
     * @return RemoteAddress
     */
    public function setTrustedProxies(array $trustedProxies)
    {
        $this->trustedProxies = $trustedProxies;
        return $this;
    }
 
    /**
     * Set the header to introspect for proxy IPs
     *
     * @param  string $header
     * @return RemoteAddress
     */
    public function setProxyHeader($header = 'X-Forwarded-For')
    {
        $this->proxyHeader = $this->normalizeProxyHeader($header);
        return $this;
    }
 
    /**
     * Returns client IP address.
     *
     * @return string IP address.
     */
    public function getIpAddress()
    {
        $ip = $this->getIpAddressFromProxy();
        if ($ip) {
            return $ip;
        }
 
        // direct IP address
        if (isset($_SERVER['REMOTE_ADDR'])) {
            $ip=$_SERVER['REMOTE_ADDR'];
            if($ip="::1")
            {
                $ip="127.0.0.1";
            }
                 
            return $ip;
        }
 
        return '';
    }
 
    /**
     * Attempt to get the IP address for a proxied client
     *
     * @see http://tools.ietf.org/html/draft-ietf-appsawg-http-forwarded-10#section-5.2
     * @return false|string
     */
    protected function getIpAddressFromProxy()
    {
        if (! $this->useProxy
            || (isset($_SERVER['REMOTE_ADDR']) && ! in_array($_SERVER['REMOTE_ADDR'], $this->trustedProxies))
        ) {
            return false;
        }
 
        $header = $this->proxyHeader;
        if (! isset($_SERVER[$header]) || empty($_SERVER[$header])) {
            return false;
        }
 
        // Extract IPs
        $ips = explode(',', $_SERVER[$header]);
        // trim, so we can compare against trusted proxies properly
        $ips = array_map('trim', $ips);
        // remove trusted proxy IPs
        $ips = array_diff($ips, $this->trustedProxies);
 
        // Any left?
        if (empty($ips)) {
            return false;
        }
 
        // Since we've removed any known, trusted proxy servers, the right-most
        // address represents the first IP we do not know about -- i.e., we do
        // not know if it is a proxy server, or a client. As such, we treat it
        // as the originating IP.
        // @see http://en.wikipedia.org/wiki/X-Forwarded-For
        $ip = array_pop($ips);
        return $ip;
    }
 
    /**
     * Normalize a header string
     *
     * Normalizes a header string to a format that is compatible with
     * $_SERVER
     *
     * @param  string $header
     * @return string
     */
    protected function normalizeProxyHeader($header)
    {
        $header = strtoupper($header);
        $header = str_replace('-', '_', $header);
        if (0 !== strpos($header, 'HTTP_')) {
            $header = 'HTTP_' . $header;
        }
        return $header;
    }
}
 
/* End of file User_agent.php */
/* Location: ./system/libraries/User_agent.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
<?php
 
// 显示所有信息,默认显示 INFO_ALL
//phpinfo();  //如果禁用,php的配置文件php.ini,将phpinfo函数从disable_functions 中移除再重启服务器即可
 
// Show just the module information. 仅仅显示PHP模块信息,
// phpinfo(8) 返回同样的结果。
// phpinfo(INFO_MODULES);
// prints e.g. 'Current PHP version: 4.1.1'
echo 'Current PHP version(PHP當前版本): ' . phpversion();
 
// prints e.g. '2.0' or nothing if the extension isn't enabled
echo phpversion('tidy');
 
// print_r(ini_get_all("pcre"));
// print_r(ini_get_all());
$indicesServer = array('PHP_SELF',
'argv',
'argc',
'GATEWAY_INTERFACE',
'SERVER_ADDR',
'SERVER_NAME',
'SERVER_SOFTWARE',
'SERVER_PROTOCOL',
'REQUEST_METHOD',
'REQUEST_TIME',
'REQUEST_TIME_FLOAT',
'QUERY_STRING',
'DOCUMENT_ROOT',
'HTTP_ACCEPT',
'HTTP_ACCEPT_CHARSET',
'HTTP_ACCEPT_ENCODING',
'HTTP_ACCEPT_LANGUAGE',
'HTTP_CONNECTION',
'HTTP_HOST',
'HTTP_REFERER',
'HTTP_USER_AGENT',
'HTTPS',
'REMOTE_ADDR',
'REMOTE_HOST',
'REMOTE_PORT',
'REMOTE_USER',
'REDIRECT_REMOTE_USER',
'SCRIPT_FILENAME',
'SERVER_ADMIN',
'SERVER_PORT',
'SERVER_SIGNATURE',
'PATH_TRANSLATED',
'SCRIPT_NAME',
'REQUEST_URI',
'PHP_AUTH_DIGEST',
'PHP_AUTH_USER',
'PHP_AUTH_PW',
'AUTH_TYPE',
'PATH_INFO',
'ORIG_PATH_INFO') ;
 
echo '<table cellpadding="10" border="1">' ;
foreach ($indicesServer as $arg) {
    if (isset($_SERVER[$arg])) {
        echo '<tr><td>'.$arg.'</td><td>' . $_SERVER[$arg] . '</td></tr>' ;
    }
    else {
        echo '<tr><td>'.$arg.'</td><td>-</td></tr>' ;
    }
}
echo '</table>' ;
 
?>

  

  

posted @   ®Geovin Du Dream Park™  阅读(659)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2011-07-12 SQL SERVER 2000 遍历父子关系数据的表(二叉树)获得所有子节点 所有父节点及节点层数函数
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示