Find the IP behind Proxy

04/14/2012 13:38 Angellinho#1
It`s working only on Apache Servers (not Litespeed) that have installed HTTP_X_REAL_IP and HTTP_X_FORWARDED_FOR .

PHP Code:
function ip() {
  
$address $_SERVER['REMOTE_ADDR'] ;
  if ( ! empty( 
$_SERVER['HTTP_X_REAL_IP'] ) ) {
   
$ip[] = 'X-Real-IP: ' $_SERVER['HTTP_X_REAL_IP'] ;
  }
  if ( ! empty( 
$_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
   
$ip[] = 'X-Forwarded-For: ' $_SERVER['HTTP_X_FORWARDED_FOR'] ;
  }

  if ( ! empty( 
$ip ) ) $address .= '(' implode','$ip ) . ')' ;

  return 
$address ;
 } 
Second way:

PHP Code:
function ip() {
  if ( isset( 
$_SERVER['HTTP_X_FORWARDED_FOR'] ) && filter_var$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP,
   
FILTER_FLAG_NO_PRIV_RANGE ) ) {
   
$_SERVER['REMOTE_ADDR'] = filter_var$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IPFILTER_FLAG_NO_PRIV_RANGE ) ;
  } else
   if ( isset( 
$_SERVER['HTTP_X_REAL_IP'] ) && filter_var$_SERVER['HTTP_X_REAL_IP'], FILTER_VALIDATE_IP,
    
FILTER_FLAG_NO_PRIV_RANGE ) ) {
    
$_SERVER['REMOTE_ADDR'] = filter_var$_SERVER['HTTP_X_REAL_IP'], FILTER_VALIDATE_IPFILTER_FLAG_NO_PRIV_RANGE ) ;
   } else {
    if ( isset( 
$_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
     
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'] ;
    }
    if ( isset( 
$_SERVER['HTTP_X_REAL_IP'] ) ) {
     
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP'] ;
    }
   }
   return 
$_SERVER['REMOTE_ADDR'] ;
 } 
You can **** yourself if you will ask where to put these codes. Means you`re not deserved to have internet access then.
04/14/2012 18:06 Whoknowsit#2
Where to put these codes? :D
04/14/2012 18:44 MostTronics© - Korb#3
HTTP_X_FORWARDED_FOR is correct
04/14/2012 20:15 Angellinho#4
Thanks and, Whoknowsit,

[Only registered and activated users can see links. Click Here To Register...]

Really?!
04/14/2012 21:27 Whoknowsit#5
Just kidding :) Nice post.
04/14/2012 23:41 Angellinho#6
Thank you.
04/15/2012 02:09 Mashkin#7
Quote:
Originally Posted by Whoknowsit View Post
Just kidding :) Nice post.
You almost got 1000 posts...

I would prefer the second way...
However, it should be clear how to access the X-Forwarded-For or X-Real-IP headers once you know they exist.

Same thing with the filter_var() function, though.