Использовать: echo getRealIP();
function getRealIP() {
$result = false;
$proxy_headers = array(
'CLIENT_IP',
'FORWARDED',
'FORWARDED_FOR',
'FORWARDED_FOR_IP',
'HTTP_CLIENT_IP',
'HTTP_FORWARDED',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED_FOR_IP',
'HTTP_PC_REMOTE_ADDR',
'HTTP_PROXY_CONNECTION',
'HTTP_VIA',
'HTTP_X_FORWARDED',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED_FOR_IP',
'HTTP_X_IMFORWARDS',
'HTTP_XROXY_CONNECTION',
'VIA',
'X_FORWARDED',
'X_FORWARDED_FOR'
);
foreach($proxy_headers as $proxy_header) {
if ($result !== false) continue;
if(isset($_SERVER[$proxy_header])) {
preg_match("/d{1,3}.d{1,3}.d{1,3}.d{1,3}/", $_SERVER[$proxy_header],$matches);
if (isset($matches[0])) {
$result = $matches[0];
continue;
}
}
else if(stristr(',', $_SERVER[$proxy_header]) !== FALSE) {
$proxy_header_temp = trim(array_shift(explode(',', $_SERVER[$proxy_header])));
if(($pos_temp = stripos($proxy_header_temp, ':')) !== FALSE) $proxy_header_temp = substr($proxy_header_temp, 0, $pos_temp);
if(preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $proxy_header_temp)){
$result = $_SERVER[$proxy_header];
continue;
}
}
}
if ($result === false){
$result = $_SERVER['REMOTE_ADDR'];
}
if (preg_match("#([0-9]+.).*#", $result, $m)){
if ($m[1] == "10." || $m[1] == "100."){
$result = $_SERVER['REMOTE_ADDR'];
}
}
if (preg_match("#([0-9]+.[0-9]+.).*#", $result, $m)){
if ($m[1] == "192.168."){
$result = $_SERVER['REMOTE_ADDR'];
}
}
return $result;
};