get ip address

Discussion in 'PHP' started by onlyican.com, Jan 11, 2006.

  1. #1
    Hey guys

    I got this code

    
    function validip($ip) {
    
       if (!empty($ip) && ip2long($ip)!=-1) {
    
           $reserved_ips = array (
    
           array('0.0.0.0','2.255.255.255'),
    
           array('10.0.0.0','10.255.255.255'),
    
           array('127.0.0.0','127.255.255.255'),
    
           array('169.254.0.0','169.254.255.255'),
    
           array('172.16.0.0','172.31.255.255'),
    
           array('192.0.2.0','192.0.2.255'),
    
           array('192.168.0.0','192.168.255.255'),
    
           array('255.255.255.0','255.255.255.255')
    
           );
    
    
    
           foreach ($reserved_ips as $r) {
    
               $min = ip2long($r[0]);
    
               $max = ip2long($r[1]);
    
               if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false;
    
           }
    
           return true;
    
       } else {
    
           return false;
    
       }
    
    }
    
    function getip() {
    
       if (validip($_SERVER["HTTP_CLIENT_IP"])) {
    
           return $_SERVER["HTTP_CLIENT_IP"];
    
       }
    
       foreach (explode(",",$_SERVER["HTTP_X_FORWARDED_FOR"]) as $ip) {
    
           if (validip(trim($ip))) {
    
               return $ip;
    
           }
    
       }
    
       if (validip($_SERVER["HTTP_X_FORWARDED"])) {
    
           return $_SERVER["HTTP_X_FORWARDED"];
    
       } elseif (validip($_SERVER["HTTP_FORWARDED_FOR"])) {
    
           return $_SERVER["HTTP_FORWARDED_FOR"];
    
       } elseif (validip($_SERVER["HTTP_FORWARDED"])) {
    
           return $_SERVER["HTTP_FORWARDED"];
    
       } elseif (validip($_SERVER["HTTP_X_FORWARDED"])) {
    
           return $_SERVER["HTTP_X_FORWARDED"];
    
       } else {
    
           return $_SERVER["REMOTE_ADDR"];
    
       }
    
    }
    
    echo getip();
    PHP:
    But there has to be a better way of doing it
     
    onlyican.com, Jan 11, 2006 IP
  2. discoverclips

    discoverclips Peon

    Messages:
    491
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if you want to retrieve the ip of the visitor
    use:

    $ip = $_SERVER['REMOTE_ADDR'];
     
    discoverclips, Jan 13, 2006 IP
  3. 2WIRE

    2WIRE Guest

    Messages:
    59
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    How did you take something so simple and make it so complicated?
     
    2WIRE, Jan 14, 2006 IP
  4. eKstreme

    eKstreme Guest

    Messages:
    131
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Depends how much "validation" you want. If you want to just make sure that the IP address is "valid", it has be 4 numbers from 0-255 separated by full stops. If you want "private" vs public addresses, then you need more code, like you did.
     
    eKstreme, Jan 14, 2006 IP
  5. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That script gets the IP Address regardless of the proxy

    $_SERVER['REMOTE_ADDR']

    Would get the proxy ip address
     
    onlyican.com, Jan 14, 2006 IP