1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

IP address issue

Discussion in 'PHP' started by siraxi, Aug 8, 2006.

  1. #1
    Hi

    I use to determine visitors address with
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    PHP:
    But lately when viewing my own IP address as displayed by this variable I'm getting the IP of Google - pertaining to a Mountain View, CA location. How can this be? I am quite far from California...

    Would you recommend using another expression in order to get the real IP?
     
    siraxi, Aug 8, 2006 IP
  2. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Cool... that means only one thing... You are working for Google ;) :D

    Thomas
     
    coderlinks, Aug 9, 2006 IP
  3. siraxi

    siraxi Peon

    Messages:
    333
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah, I wish!

    Could Googlebot be the cause? Weird.
     
    siraxi, Aug 9, 2006 IP
  4. kingofdollars

    kingofdollars Well-Known Member

    Messages:
    97
    Likes Received:
    12
    Best Answers:
    1
    Trophy Points:
    110
    #4
    could be due to proxy?
     
    kingofdollars, Aug 9, 2006 IP
  5. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Are you using Google Web Accelerator?

    If you are going through a proxy, then maybe the original user IP address is stored somewhere in the HTTP headers. Make a call to apache_request_headers(), and have a rummage in the array that gets returned.

    Cryo.
     
    Cryogenius, Aug 9, 2006 IP
  6. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I have been using this function:

    
    function get_client_ip() {
    	if (isset ($_SERVER['HTTP_X_FORWARDED_FOR']))
    		$ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    	else
    		$ipAddress = $_SERVER['REMOTE_ADDR'];
    	return $ipAddress;
    }
    
    Code (markup):
    Maybe it will help.
     
    clancey, Aug 9, 2006 IP
  7. siraxi

    siraxi Peon

    Messages:
    333
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Oh, yes, that's it: I actually am using Google Web Accelerator! This must be the explanation. Many thanks.

    And I'll use the suggested function. Many thanks too.
     
    siraxi, Aug 9, 2006 IP
  8. webviz

    webviz Peon

    Messages:
    216
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    <?php
    function get_real_ip()
    {
       // No IP found (will be overwritten by for
       // if any IP is found behind a firewall)
       $ip = FALSE;
       
       // If HTTP_CLIENT_IP is set, then give it priority
       if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
           $ip = $_SERVER["HTTP_CLIENT_IP"];
       }
       
       // User is behind a proxy and check that we discard RFC1918 IP addresses
       // if they are behind a proxy then only figure out which IP belongs to the
       // user.  Might not need any more hackin if there is a squid reverse proxy
       // infront of apache.
       if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    
           // Put the IP's into an array which we shall work with shortly.
           $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
           if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
    
           for ($i = 0; $i < count($ips); $i++) {
               // Skip RFC 1918 IP's 10.0.0.0/8, 172.16.0.0/12 and
               // 192.168.0.0/16 -- jim kill me later with my regexp pattern
               // below.
               if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {
                   if (version_compare(phpversion(), "5.0.0", ">=")) {
                       if (ip2long($ips[$i]) != false) {
                           $ip = $ips[$i];
                           break;
                       }
                   } else {
                       if (ip2long($ips[$i]) != -1) {
                           $ip = $ips[$i];
                           break;
                       }
                   }
               }
           }
       }
    
       // Return with the found IP or the remote address
       return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
    }
    ?>
    
    PHP:
    If you find my stuff helpful, give me good rep... NOW! ;)
     
    webviz, Aug 9, 2006 IP
    vishwaa likes this.