any function to trace ip address correctly....?

Discussion in 'PHP' started by ravianz, Nov 2, 2005.

  1. #1
    Hi
    i need to trace the ip address of my visitors when they login; i found a function

    getenv("REMOTE_ADDR");
    PHP:
    but when i uploaded a file having this function and ran on my computer i got my ip address: 202.165.255.17
    but at the same time i checked ipconfig in cmd and it gave me a different ip:
    202.165.254.118

    what is the reason that ip address is not correctly given by the function or i should use some other function;
     
    ravianz, Nov 2, 2005 IP
  2. mnemtsas

    mnemtsas Super Dud

    Messages:
    497
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Maybe you are accessing the internet via a proxy and the 202.165.255.17 is the proxy address.
     
    mnemtsas, Nov 2, 2005 IP
  3. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #3
    Also, it's cleaner to use
    $_SERVER['REMOTE_ADDR']
    PHP:
     
    digitalpoint, Nov 3, 2005 IP
  4. ravianz

    ravianz Notable Member

    Messages:
    1,536
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    250
    #4
    it may be; i am using dialup and on other isp i get the correct ip address; but with the previous isp my hosting CPanel is showing my correct ip address; what may be the reason...
     
    ravianz, Nov 3, 2005 IP
  5. Nokia999

    Nokia999 Guest

    Messages:
    1,488
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yup it happens sometime.
     
    Nokia999, Nov 30, 2005 IP
  6. ravianz

    ravianz Notable Member

    Messages:
    1,536
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    250
    #6

    I have found another funtion which is better in some situations; so i am using the two functions with conditional logic ;

    two functions i am using are:


    $_SERVER['HTTP_X_FORWARDED_FOR']
    
    PHP:
    and

    $_SERVER['REMOTE_ADDR'] 
    PHP:
     
    ravianz, Nov 30, 2005 IP
  7. cornelius

    cornelius Peon

    Messages:
    206
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    here you go a super duper function for you

    /*** FUNCTION GETIP ***/
    function getip() {
    	if (isSet($_SERVER)) {
     		if (isSet($_SERVER["HTTP_X_FORWARDED_FOR"])) {
      			$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
     		} 
    		else if (isSet($_SERVER["HTTP_CLIENT_IP"])) {
      			$realip = $_SERVER["HTTP_CLIENT_IP"];
     		} 
    		else {
      			$realip = $_SERVER["REMOTE_ADDR"];
     		}
    
    	} 
    	else {
     		if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
      			$realip = getenv( 'HTTP_X_FORWARDED_FOR' );
     		} 
    		else if ( getenv( 'HTTP_CLIENT_IP' ) ) {
      			$realip = getenv( 'HTTP_CLIENT_IP' );
     		} 
    		else {
      			$realip = getenv( 'REMOTE_ADDR' );
     		}
    	}
    return $realip; 
    }
    /*** FUNCTION GETIP ***/
    PHP:
     
    cornelius, Dec 2, 2005 IP
  8. keral82

    keral82 Banned

    Messages:
    781
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    $_SERVER['REMOTE_ADDR'] works fine for my codes. Have you gave it a try. There's also another command i don't remember it right now but it is like HTTP_REFERER or something. You might find it on google.
     
    keral82, Dec 4, 2005 IP
  9. cornelius

    cornelius Peon

    Messages:
    206
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #9
    just use the function i did there

    it works like a charm
     
    cornelius, Dec 4, 2005 IP
  10. Alexander

    Alexander Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    hmmmm... really super, but

    It's necessary to TRACE user, and you'll get 192.168.255.22 from X_FORWARDED_FOR. Better store all.

    function getip() {
    	$ip = array_unique(array('',$_SERVER["REMOTE_ADDR"],$_SERVER["HTTP_X_FORWARDED_FOR"],$_SERVER["HTTP_CLIENT_IP"]));
    	array_shift($ip);
    	return(implode(',',$ip));
    }
    PHP:
     
    Alexander, Dec 5, 2005 IP
  11. Shodan5

    Shodan5 Guest

    Messages:
    53
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #11
    thank you i needed this
     
    Shodan5, Dec 5, 2005 IP