1

We have 50+ computers in our office and each computer have separate Internet connection from local service provider. Now in our website we want to get each computer information(I don't know without ip have any other ways) at the time of user registration.

So as a normal method we used $ip = $_SERVER['SERVER_ADDR']; but it display same ip for all computer/connection.

In here stackoverflow, I found many solution to get a real ip. But I confused, what will be the better for us to get each computer ip in registration process of our website.

Method 1:

getenv() is used to get the value of an environment variable in PHP.

// Function to get the client IP address
function get_client_ip() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
    $ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
    $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
    $ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
    $ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
   $ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
    $ipaddress = getenv('REMOTE_ADDR');
else
    $ipaddress = 'UNKNOWN';
return $ipaddress;
}

$_SERVER is an array that contains server variables created by the web server.

// Function to get the client IP address
function get_client_ip() {
$ipaddress = '';
if ($_SERVER['HTTP_CLIENT_IP'])
    $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['HTTP_X_FORWARDED_FOR'])
    $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
    $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if($_SERVER['HTTP_FORWARDED_FOR'])
    $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if($_SERVER['HTTP_FORWARDED'])
    $ipaddress = $_SERVER['HTTP_FORWARDED'];
else if($_SERVER['REMOTE_ADDR'])
    $ipaddress = $_SERVER['REMOTE_ADDR'];
else
    $ipaddress = 'UNKNOWN';
return $ipaddress;
}

Method 2:

$host= gethostname();
$ip = gethostbyname($host);

Method 3:

$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');

Method 4: (most advanced method)

function get_ip_address() {
// check for shared internet/ISP IP
if (!empty($_SERVER['HTTP_CLIENT_IP']) && validate_ip($_SERVER['HTTP_CLIENT_IP'])) {
    return $_SERVER['HTTP_CLIENT_IP'];
}

// check for IPs passing through proxies
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    // check if multiple ips exist in var
    if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false) {
        $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        foreach ($iplist as $ip) {
            if (validate_ip($ip))
                return $ip;
        }
    } else {
        if (validate_ip($_SERVER['HTTP_X_FORWARDED_FOR']))
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
}
if (!empty($_SERVER['HTTP_X_FORWARDED']) && validate_ip($_SERVER['HTTP_X_FORWARDED']))
    return $_SERVER['HTTP_X_FORWARDED'];
if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
    return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
    return $_SERVER['HTTP_FORWARDED_FOR'];
if (!empty($_SERVER['HTTP_FORWARDED']) && validate_ip($_SERVER['HTTP_FORWARDED']))
    return $_SERVER['HTTP_FORWARDED'];

// return unreliable ip since all else failed
return $_SERVER['REMOTE_ADDR'];
}

/**
 * Ensures an ip address is both a valid IP and does not fall within
 * a private network range.
 */
function validate_ip($ip) {
if (strtolower($ip) === 'unknown')
    return false;

// generate ipv4 network address
$ip = ip2long($ip);

// if the ip is set and not equivalent to 255.255.255.255
if ($ip !== false && $ip !== -1) {
    // make sure to get unsigned long representation of ip
    // due to discrepancies between 32 and 64 bit OSes and
    // signed numbers (ints default to signed in PHP)
    $ip = sprintf('%u', $ip);
    // do private network range checking
    if ($ip >= 0 && $ip <= 50331647) return false;
    if ($ip >= 167772160 && $ip <= 184549375) return false;
    if ($ip >= 2130706432 && $ip <= 2147483647) return false;
    if ($ip >= 2851995648 && $ip <= 2852061183) return false;
    if ($ip >= 2886729728 && $ip <= 2887778303) return false;
    if ($ip >;= 3221225984 && $ip <= 3221226239) return false;
    if ($ip >;= 3232235520 && $ip <= 3232301055) return false;
    if ($ip >= 4294967040) return false;
}
return true;
}
koc
  • 955
  • 8
  • 26
  • You can also use Remote Host's Name instead of ip-address and you can easily fetch hostname by using `gethostname();` function. – Gopal Joshi Mar 29 '15 at 03:18
  • Thank u sir, I am trying to know about this . . . . – koc Mar 29 '15 at 03:20
  • it does not sound like they " each computer have separate Internet connection from local service provider." rather they share one –  Mar 29 '15 at 03:20

2 Answers2

0

Methods 1-3 pretty much do the exact same thing. getenv and $_SERVER are pretty much the same as noted here.

Method 4 is a bit more advanced, it checks and validates to verify valid ip addresses before returning them. I think it's the best because it eliminates bad ip addresses from being returned from faulty proxies that could screw up code later on if you're expecting an ip address (and getting something completely different).

My suggestion: Use phpinfo(); on one of the machine and see what variables you'd like to grab. These scripts do nothing more than loop through known alternative http headers for ip addresses (Such as if they're behind a proxy. You get the proxy's ip address from REMOTE_ADDR, but the proxy says "Hey, I'm acting like a proxy, this is the guy who's connected to me: HTTP_FORWARDED_FOR").

Community
  • 1
  • 1
0

Through sql server you can get ip address from below query.

SELECT SERVERPROPERTY('ComputerNamePhysicalNetBIOS') [Machine Name]
   ,SERVERPROPERTY('InstanceName') AS [Instance Name]
   ,LOCAL_NET_ADDRESS AS [IP Address Of SQL Server]
   ,CLIENT_NET_ADDRESS AS [IP Address Of Client]
 FROM SYS.DM_EXEC_CONNECTIONS 
user3864233
  • 514
  • 3
  • 12