-2
if ( getenv('HTTP_CLIENT_IP') ) {
    $ipaddress = getenv('HTTP_CLIENT_IP');
} else if( getenv('HTTP_X_FORWARDED_FOR') ) {
    $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    $ipaddress = explode(",", $ipaddress);
    $ipaddress = $ipaddress[0];
} 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';
}

How do I get real ip from CDN? I try to google it, but there is no any post about this. anyone can help?

Benjamin W
  • 2,658
  • 7
  • 26
  • 48

1 Answers1

-1

Try using with $_SERVER

$ipaddress = $_SERVER('HTTP_CLIENT_IP');

Sometime getenv does not return the correct IP address, so we can use $_SERVER instead.

Pramod Patil
  • 2,704
  • 2
  • 14
  • 20
  • how aboiut getenv('HTTP_CLIENT_IP') – Benjamin W Mar 28 '17 at 03:34
  • both the function are same and for IP address we have $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] – Pramod Patil Mar 28 '17 at 03:37
  • @PramodPatil it depends entirely on what IP the poster actually wants which is not clear from the question at all. – pvg Mar 28 '17 at 03:38
  • @pvg You havent got the question please check again.If you have downvoted because of that please upvote. – Pramod Patil Mar 28 '17 at 03:43
  • No. It's a completely unclear question. The code shown tries to extract the 'original' by a cascade of comparisons, yours just picks one of the (several) nonstandards headers. See: http://stackoverflow.com/questions/7445592/what-is-the-difference-between-http-client-ip-and-http-x-forwarded-for It's not equivalent and, again, it's not clear what the OP is asking. Both the question and answer are of poor quality. – pvg Mar 28 '17 at 03:45
  • Ok..Please downvote this as well for your satisfaction..http://stackoverflow.com/questions/15699101/get-the-client-ip-address-using-php – Pramod Patil Mar 28 '17 at 03:49
  • This isn't even valid PHP syntax! –  Mar 28 '17 at 03:55