Get Client IP

Merchant need to pass clientIp parameter when calling creating order endpoint, hopefully this document will be helpful.

If No Proxy Exist

If there is no proxy at merchant's frontend, just use REMOTE_ADDR to get client ip.

If Proxy Exist

nginx proxy config

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Apache proxy config

vi /usr/local/apache/conf/httpd.conf
Include conf/extra/httpd-remoteip.conf
vi /usr/local/apache/conf/extra/httpd-remoteip.conf
LoadModule remoteip_module modules/mod_remoteip.so
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 127.0.0.1

php sample code

function real_ip()
{
    static $realIp = NULL;

    if ($realIp !== NULL)
    {
        return $realIp;
    }

    if (isset($_SERVER))
    {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);

            foreach ($arr AS $ip)
            {
                $ip = trim($ip);

                if ($ip != 'unknown')
                {
                    $realIp = $ip;

                    break;
                }
            }
        }
        elseif (isset($_SERVER['HTTP_CLIENT_IP']))
        {
            $realIp = $_SERVER['HTTP_CLIENT_IP'];
        }
        else
        {
            if (isset($_SERVER['REMOTE_ADDR']))
            {
                $realIp = $_SERVER['REMOTE_ADDR'];
            }
            else
            {
                $realIp = '0.0.0.0';
            }
        }
    }
    else
    {
        if (getenv('HTTP_X_FORWARDED_FOR'))
        {
            $realIp = getenv('HTTP_X_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_CLIENT_IP'))
        {
            $realIp = getenv('HTTP_CLIENT_IP');
        }
        else
        {
            $realIp = getenv('REMOTE_ADDR');
        }
    }

    preg_match("/[\d\.]{7,15}/", $realIp, $onlineIp);
    $realIp = !empty($onlineIp[0]) ? $onlineIp[0] : '0.0.0.0';

    return $realIp;
}
Last Updated: