我确实跟踪了用户的“真实”IP,如果他有一个代理发送真实IP的标题…是否有更好的解决方案,甚至更多的标题?
原文链接:https://www.f2er.com/php/133944.html由于此函数在脚本中经常使用,因此它必须非常快,并且它似乎不在那个星座中:/
我提出了一些建议,但无法实现:
>将标题放在顺序中,最常用的是“在野外”,使函数快速完成
>使IP的pre_match检测更快
===
function get_real_ip() { $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(isset($_SERVER[$proxy_header]) && 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}$/",$_SERVER[$proxy_header])) /* HEADER ist gesetzt und dies ist eine gültige IP */ { return $_SERVER[$proxy_header]; } else if(stristr(',',$_SERVER[$proxy_header]) !== FALSE) /* Behandle mehrere IPs in einer Anfrage(z.B.: X-Forwarded-For: client1,proxy1,proxy2) */ { $proxy_header_temp = trim(array_shift(explode(',$_SERVER[$proxy_header]))); /* Teile in einzelne IPs,gib die letzte zurück und entferne Leerzeichen */ if(($pos_temp = stripos($proxy_header_temp,':')) !== FALSE) $proxy_header_temp = substr($proxy_header_temp,$pos_temp); /* Entferne den Port */ 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) return $proxy_header_temp; } } return $_SERVER['REMOTE_ADDR']; }