te dejo una que uso yo
Código PHP:
function optieneIP()
{
// Mira proxy
$ip="";
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$ips=preg_split("/[\s,]+/", $_SERVER["HTTP_X_FORWARDED_FOR"]);
foreach ($ips as $tempip){
$ip=$tempip;
// Comprobacion para evitar IPs privadas.
if ((strncmp($ip,'10.',3) == 0) || (strncmp($ip,'192.168.',8) == 0) || (strncmp($ip,'172.16.',7) == 0) ||
(strncmp($ip,'172.17.',7) == 0) || (strncmp($ip,'172.18.',7) == 0) || (strncmp($ip,'172.19.',7) == 0) ||
(strncmp($ip,'172.20.',7) == 0) || (strncmp($ip,'172.21.',7) == 0) || (strncmp($ip,'172.22.',7) == 0) ||
(strncmp($ip,'172.23.',7) == 0) || (strncmp($ip,'172.24.',7) == 0) || (strncmp($ip,'172.25.',7) == 0) ||
(strncmp($ip,'172.26.',7) == 0) || (strncmp($ip,'172.27.',7) == 0) || (strncmp($ip,'172.28.',7) == 0) ||
(strncmp($ip,'172.29.',7) == 0) || (strncmp($ip,'172.30.',7) == 0) || (strncmp($ip,'172.31.',7) == 0) )
{
continue;
}
break;
}
// Comprobacion para evitar IPs privadas.
if ((strncmp($ip,'10.',3) == 0) || (strncmp($ip,'192.168.',8) == 0) || (strncmp($ip,'172.16.',7) == 0) ||
(strncmp($ip,'172.17.',7) == 0) || (strncmp($ip,'172.18.',7) == 0) || (strncmp($ip,'172.19.',7) == 0) ||
(strncmp($ip,'172.20.',7) == 0) || (strncmp($ip,'172.21.',7) == 0) || (strncmp($ip,'172.22.',7) == 0) ||
(strncmp($ip,'172.23.',7) == 0) || (strncmp($ip,'172.24.',7) == 0) || (strncmp($ip,'172.25.',7) == 0) ||
(strncmp($ip,'172.26.',7) == 0) || (strncmp($ip,'172.27.',7) == 0) || (strncmp($ip,'172.28.',7) == 0) ||
(strncmp($ip,'172.29.',7) == 0) || (strncmp($ip,'172.30.',7) == 0) || (strncmp($ip,'172.31.',7) == 0) ){
$ip=$_SERVER["REMOTE_ADDR"];
}
}else{
$ip=$_SERVER["REMOTE_ADDR"];
}
return $ip;
}*/