How to get user IP address in PHP the most reliable way - header spoofing fixed

Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Seçkin Üye
Katılım
17 Mar 2020
Mesajlar
298
Tepki puanı
26
Ödüller
5
Yaş
27
6 HİZMET YILI
Not my solution, but it's certainly the best way to get IP address in PHP. It really helped me when users were header spoofing and other exploits, but this is the proper way.
function get_ip_address(){
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
if (array_key_exists($key, $_SERVER) === true){
foreach (explode(',', $_SERVER[$key]) as $ip){
$ip = trim($ip); // just to be safe

if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
return $ip;
}
}
}
}
}
 
Moderatörün son düzenlenenleri:
The goal of all life is DEATH
Süper Üye
Katılım
9 Mar 2018
Mesajlar
860
Çözümler
6
Tepki puanı
73
Ödüller
7
8 HİZMET YILI
thank you for sharing this ;) keep up the good work bro
 
Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Üst