获取客户端ip

 1 <?php
 2 namespace Eoffcn\Util;
 3 class Network {
 4     /**
 5      * 获取ip
 6      * @return string
 7      */
 8     public static function getIP() {
 9         $ip = false;
10         if (!empty ($_SERVER ["HTTP_CLIENT_IP"])) {
11             $ip = $_SERVER ["HTTP_CLIENT_IP"];
12         }
13         if (!empty ($_SERVER ['HTTP_X_FORWARDED_FOR'])) {
14             $ips = explode(", ", $_SERVER ['HTTP_X_FORWARDED_FOR']);
15             if ($ip) {
16                 array_unshift($ips, $ip);
17                 $ip = FALSE;
18             }
19             for ($i = 0; $i < count($ips); $i++) {
20                 if (!preg_match("/^(10|172\.16|192\.168)\./i", $ips [$i])) {
21                     $ip = $ips [$i];
22                     break;
23                 }
24             }
25         }
26         return ($ip ? $ip : $_SERVER ['REMOTE_ADDR']);
27     }
28 
29     /**
30      * 获得当前的url
31      * @return string
32      */
33     public static function getCurrentURL() {
34         $scheme = 'http';
35         if (self::usingHTTPS()) {
36             $scheme .= 's';
37         }
38 
39         return sprintf('%s://%s%s', $scheme, $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI']);
40     }
41 
42     /**
43      * 获得当前的域名
44      * @return string
45      */
46     public static function getCurrentDomain() {
47         return isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
48     }
49 
50     /**
51      * 是否使用https
52      * @return bool
53      */
54     public static function usingHTTPS() {
55         if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
56             return true;
57         } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
58             return true;
59         }
60         return false;
61     }
62 }

 

posted @ 2018-09-11 14:41  helloworldlee  阅读(396)  评论(0编辑  收藏  举报