FIX for getting IP of visitor

Back

TO get the real IP of the visitor just open your configuration file e.g config.php and paste what is mentioned below. If you dont have a configuration file or want to get the ip at a specific place e.g index.php or whatever then just copy and paste the below code on top of your script / file.

 

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;}}}}}

$_SERVER['REMOTE_ADDR']= get_ip_address();

 

This will do the job.