Php src get remote addr: Unterschied zwischen den Versionen
Aus My Wiki
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
| Zeile 1: | Zeile 1: | ||
< | <code> | ||
function get_remote_addr() | function get_remote_addr() | ||
{ | { | ||
| Zeile 28: | Zeile 28: | ||
</ | </code> | ||
Aktuelle Version vom 1. Januar 1970, 00:33 Uhr
function get_remote_addr()
{
global $HTTP_SERVER_VARS;
$GLOBAL = ( isset($HTTP_SERVER_VARS) ? $HTTP_SERVER_VARS : ( isset($_SERVER) ? $_SERVER : ) );
$http_x_forwarded_for = ( isset($GLOBAL["HTTP_X_FORWARDED_FOR"]) ? $GLOBAL["HTTP_X_FORWARDED_FOR"] : );
$remote_addr = ( isset($GLOBAL["REMOTE_ADDR"]) ? $GLOBAL["REMOTE_ADDR"] : );
if ( ! empty($http_x_forwarded_for) ) {
foreach ( explode( ',', $http_x_forwarded_for ) as $forward ) {
// test if $forward is an IP-address
if ( preg_match_all( "/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/", $forward, $IP ) ) {
// continue if $IP is a private IP-address
if ( ( $IP[1][0]==10 )
|| ( $IP[1][0]==192 && $IP[2][0]==168 )
|| ( $IP[1][0]==172 && $IP[2][0]>=16 && $IP[2][0]<=31 )
) {
continue;
}
// return if $IP is a public IP-address
return $forward;
}
}
}
return $remote_addr;
}