0

I am using the following function for getting the ip address of client.

function get_client_ip() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_X_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else 

    if(isset($_SERVER['REMOTE_ADDR']))
        $ipaddress = $_SERVER['REMOTE_ADDR'];

    else
        $ipaddress = 'UNKNOWN';

    return $ipaddress;
}

But I check the results it gives me IP address different than my machine IP address on server.(I think it's the IP of my organization server) Any solution I can get the real IP address of my client. Edit for Possible duplicate: I have read the answer of possible duplicate question. My problem is that It's not giving me the ip address of my machine.

Edit-2 Live link http://stashad.com/nodegates/voter.php?incr=blockvotes

sheldon cooper
  • 455
  • 2
  • 8
  • 22

2 Answers2

0

try this snippet.,

if your computer is localhost or connected on network its shows their original IP

function getIP($ip = null, $deep_detect = TRUE){
    if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
        $ip = $_SERVER["REMOTE_ADDR"];
        if ($deep_detect) {
            if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
    } else {
        $ip = $_SERVER["REMOTE_ADDR"];
    }
    return $ip;
}

usage

echo getIP();

if deep_detect set to be TRUE check client IP is forwarded or not

echo getIP(null,true);
-1

Your computer is not directly connecting to the server; it is connecting via an intermediary device--something like a router, or a gateway in a large company.

The server can generally only get the IP address of the connecting device. In some cases things like load balancers will add headers in the request that include an originating IP address (e.g. HTTP_X_FORWARDED_FOR) but I don't think these will generally include the private IP address of the machine that made the request.

If you look at the diagram below, you are one of the computers but the router makes the actual connection to the server, rather than your computer itself.

Network diagram

StuBez
  • 1,346
  • 14
  • 21