zoukankan      html  css  js  c++  java
  • 获取客户端ip

     1 <?php
     2 namespace EoffcnUtil;
     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 }
  • 相关阅读:
    MathType输入框怎么调整
    几何画板中去除画出的线段的教程
    MathType怎么编辑半开半闭区间
    几何画板给月牙图形填充颜色的技巧
    MathType调整矩阵分隔线粗细的方法
    帮你深入理解OAuth2.0协议
    phalapi
    Spring松耦合实例
    让前端工程师糟心的正则表达式到底是什么?
    composer安装
  • 原文地址:https://www.cnblogs.com/hellohell/p/9627538.html
Copyright © 2011-2022 走看看