nginx 

  proxy_set_header X-Real-IP $remote_addr;

  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  proxy_set_header Host $host;

  real_ip_header    X-Forwarded-For;

  real_ip_recursive on;

 

php 

<?php

echo long2ip(_get_ip());

function _get_ip($full = false) {

        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {

            if ($full == true)

                return sprintf("%u\n", ip2long($_SERVER["HTTP_X_FORWARDED_FOR"]));

            $tmp_ips = explode(',', $_SERVER["HTTP_X_FORWARDED_FOR"]);

            $ip = array_shift($tmp_ips);

        }

        else if (isset($_SERVER["HTTP_CLIENT_IP"]))

            $ip = $_SERVER["HTTP_CLIENT_IP"];

        else if (isset($_SERVER["REMOTE_ADDR"]))

            $ip = $_SERVER["REMOTE_ADDR"];

        else if (getenv("HTTP_X_FORWARDED_FOR"))

            $ip = getenv("HTTP_X_FORWARDED_FOR");

        else if (getenv("HTTP_CLIENT_IP"))

            $ip = getenv("HTTP_CLIENT_IP");

        else if (getenv("REMOTE_ADDR"))

            $ip = getenv("REMOTE_ADDR");

        else

            $ip = "Unknown";

        return sprintf("%u\n", ip2long($ip));

    }


声明:此文系舞林cuznwww.wulinlw.org)原创稿件,转载请保留版权

et_highlighter