Trying to pull JSON data

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:


# 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;
    } 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, '' . $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.

// Print the data out onto the page.
echo $data;
    //$data = fopen('' . $ip_address . '&fields=country,region,city');
    //return $data;

// Print the data out onto the page.
//return $data;


echo getIpAddress();


Any assistance is greatly appreciated!

Well, since IP addresses are easy to spoof, you can not guarantee that the incoming IP is truly valid.
But, it appears you have covered all of the possible options. One thing that you can do to debug this is to review the entire $_SERVER array to see what is actually coming in and then adjust your code to work with the info coming in. Just create a test page that shows the incoming data. Something like this:

echo "Incoming data: <pre>" . print_r($_SERVER, 1) . "</pre>";

Something like that would show you exactly what is coming in from the remote person’s IP.
( Along with a lot of other data. ) You can use that to see what shows up in the server array. But, of course, you would need to have others try it so that you see what their IP is throwing info to you.

Not a solution, but, help debugging the issue. Hope this helps!

Yes sir that helps!

Glad it helped. I also think you can look at these PHP functions which might help also:
gethostbyaddr ( ip_address )
gethostbyname ( hostname )

These might be handy to verify items to see if the resulting IP’s match where you think they should.

Again, not a solution, but, should help you further adjust your “getIpAddress” function. Maybe when you finish it, you can post it here to assist others later on. But, this really isn’t a JSON issue. Good luck!

Thanks my friend! Much appreciated :+1:

Sponsor our Newsletter | Privacy Policy | Terms of Service