Getting ip address of visitor then sending request to geoip vendor. It seems all that displays on screen is the visitor’s ip address. The rest is ignored. What am i doing wrong?
Here is my code:
<?php
##################
# Gets IP address.
##################
function getIpAddress()
{
$ipAddress = '';
if (! empty($_SERVER['HTTP_CLIENT_IP'])) {
// to get shared ISP IP address
$ipAddress = $_SERVER['HTTP_CLIENT_IP'];
} else if (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
// check for IPs passing through proxy servers
// check if multiple IP addresses are set and take the first one
$ipAddressList = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
foreach ($ipAddressList as $ip) {
if (! empty($ip)) {
// if you prefer, you can check for valid IP address here
$ipAddress = $ip;
break;
}
}
} else if (! empty($_SERVER['HTTP_X_FORWARDED'])) {
$ipAddress = $_SERVER['HTTP_X_FORWARDED'];
} else if (! empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) {
$ipAddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
} else if (! empty($_SERVER['HTTP_FORWARDED_FOR'])) {
$ipAddress = $_SERVER['HTTP_FORWARDED_FOR'];
} else if (! empty($_SERVER['HTTP_FORWARDED'])) {
$ipAddress = $_SERVER['HTTP_FORWARDED'];
} else if (! empty($_SERVER['REMOTE_ADDR'])) {
$ipAddress = $_SERVER['REMOTE_ADDR'];
}
return $ipAddress;
// Initialize cURL.
$ch = curl_init();
// Set the URL that you want to GET by using the CURLOPT_URL option.
curl_setopt($ch, CURLOPT_URL, 'https://ipgeolocation.abstractapi.com/v1/?api_key=6b67bc29622248e3a1b49716b6a1f44e&' . $ip_address . '&fields=country,region,city');
// Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set CURLOPT_FOLLOWLOCATION to true to follow redirects.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Execute the request.
$data = curl_exec($ch);
// Close the cURL handle.
curl_close($ch);
// Print the data out onto the page.
echo $data;
//$data = fopen('https://ipgeolocation.abstractapi.com/v1/?api_key=6b67bc29622248e3a1b49716b6a1f44e&' . $ip_address . '&fields=country,region,city');
//return $data;
// Print the data out onto the page.
//return $data;
}
echo getIpAddress();
?>
Any assistance is greatly appreciated!