-3

Please take a look at my code.

$http_client_ip = $_SERVER['HTTP_CLIENT_IP']; 
$http_x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR']; 
$remote_addr = $_SERVER['REMOTE_ADDR'];

if(!empty($http_client_ip)) {
    $ip_address = $http_client_ip;
} else if(!empty($http_x_forwarded_for)) {
    $ip_address = $http_x_forwarded_for;
} else {
    $ip_address = $remote_addr;
}

echo "IP Address: {$ip_address}."

Everything should work fine, but I'm getting these notifications when I'm running the script.

I can't understand why... I'm a beginner. Thanks in advance.

1 Answers1

1

If $_SERVER has no index named HTTP_CLIENT_IP, HTTP_X_FORWARDED_FOR or REMOTE_ADDR, you will get this error.

Before accessing one of these indices in the $_SERVER array, you should check if that index exists. You can do this using isset().

For example:

if (!isset($_SERVER['HTTP_CLIENT_IP'])) {
    // index doesn't exist, handle error if necessary
}
else {
    // keep going
}
Conor Taylor
  • 2,998
  • 7
  • 37
  • 69