Get real ip address php

I'm using this PHP code to get a visitor's IP address:


But, I can't get the real IP address from visitors when they are using a proxy. Is there any way to get a visitor's IP address in this case?

reformed

4,24510 gold badges59 silver badges83 bronze badges

asked Nov 30, 2012 at 13:58

4

Try this php code.


Mario

1,2943 gold badges20 silver badges39 bronze badges

answered Nov 30, 2012 at 14:01

10

This is the most common technique I've seen:

function getUserIP[] {
    if[ array_key_exists['HTTP_X_FORWARDED_FOR', $_SERVER] && !empty[$_SERVER['HTTP_X_FORWARDED_FOR']] ] {
        if [strpos[$_SERVER['HTTP_X_FORWARDED_FOR'], ',']>0] {
            $addr = explode[",",$_SERVER['HTTP_X_FORWARDED_FOR']];
            return trim[$addr[0]];
        } else {
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
    }
    else {
        return $_SERVER['REMOTE_ADDR'];
    }
}

Note that it does not guarantee it you will get always the correct user IP because there are many ways to hide it.

answered Nov 30, 2012 at 14:08

TeneffTeneff

27.8k9 gold badges64 silver badges96 bronze badges

4

This is my approach:

 function getRealUserIp[]{
    switch[true]{
      case [!empty[$_SERVER['HTTP_X_REAL_IP']]] : return $_SERVER['HTTP_X_REAL_IP'];
      case [!empty[$_SERVER['HTTP_CLIENT_IP']]] : return $_SERVER['HTTP_CLIENT_IP'];
      case [!empty[$_SERVER['HTTP_X_FORWARDED_FOR']]] : return $_SERVER['HTTP_X_FORWARDED_FOR'];
      default : return $_SERVER['REMOTE_ADDR'];
    }
 }

How to use:

$ip = getRealUserIp[];

answered Mar 3, 2015 at 23:36

El ceroEl cero

5694 silver badges13 bronze badges

7

Proxies may send a HTTP_X_FORWARDED_FOR header but even that is optional.

Also keep in mind that visitors may share IP addresses; University networks, large companies and third-world/low-budget ISPs tend to share IPs over many users.

answered Nov 30, 2012 at 13:59

HalcyonHalcyon

56.6k10 gold badges87 silver badges126 bronze badges

2

apply this code for get the ipaddress:

    if [getenv['HTTP_X_FORWARDED_FOR']] { $pipaddress = getenv['HTTP_X_FORWARDED_FOR'];
 $ipaddress = getenv['REMOTE_ADDR']; 
    echo "Your Proxy IP address is : ".$pipaddress. "[via $ipaddress]" ; } 
    else { $ipaddress = getenv['REMOTE_ADDR']; echo "Your IP address is : $ipaddress"; }
    ------------------------------------------------------------------------

Arpit Patel

9,2155 gold badges63 silver badges72 bronze badges

answered Feb 22, 2016 at 8:51

This is my function.

benefits :

  • Work if $_SERVER was not available.
  • Filter private and/or reserved IPs;
  • Process all forwarded IPs in X_FORWARDED_FOR
  • Compatible with CloudFlare
  • Can set a default if no valid IP found!
  • Short & Simple !
/**
 * Get real user ip
 *
 * Usage sample:
 * GetRealUserIp[];
 * GetRealUserIp['ERROR',FILTER_FLAG_NO_RES_RANGE];
 * 
 * @param string $default default return value if no valid ip found
 * @param int    $filter_options filter options. default is FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
 *
 * @return string real user ip
 */

function GetRealUserIp[$default = NULL, $filter_options = 12582912] {
    $HTTP_X_FORWARDED_FOR = isset[$_SERVER]? $_SERVER["HTTP_X_FORWARDED_FOR"]:getenv['HTTP_X_FORWARDED_FOR'];
    $HTTP_CLIENT_IP = isset[$_SERVER]?$_SERVER["HTTP_CLIENT_IP"]:getenv['HTTP_CLIENT_IP'];
    $HTTP_CF_CONNECTING_IP = isset[$_SERVER]?$_SERVER["HTTP_CF_CONNECTING_IP"]:getenv['HTTP_CF_CONNECTING_IP'];
    $REMOTE_ADDR = isset[$_SERVER]?$_SERVER["REMOTE_ADDR"]:getenv['REMOTE_ADDR'];

    $all_ips = explode[",", "$HTTP_X_FORWARDED_FOR,$HTTP_CLIENT_IP,$HTTP_CF_CONNECTING_IP,$REMOTE_ADDR"];
    foreach [$all_ips as $ip] {
        if [$ip = filter_var[$ip, FILTER_VALIDATE_IP, $filter_options]]
            break;
    }
    return $ip?$ip:$default;
}

answered May 2, 2017 at 12:47

If the Proxy is which you trust, you can try: [Assume the Proxy IP is 151.101.2.10]

Chủ Đề