0

I am trying to get country name by Geo-location. so i used Geo plugin but not working good. its show all good by browser url. but when i am trying to set response by php its not return anything.

I don't know what is issue with my code.

Geoip Url : http://www.geoplugin.net/php.gp?ip=xx.xx.xx.xx

require_once('geoplugin.class.php');

 $geoplugin = new geoPlugin();
    $geoplugin->locate();
    if($geoplugin->countryCode){
        $_SESSION['cCode'] = $geoplugin->countryCode;
    }else{
        $_SESSION['cCode'] = "US";
    }
6
  • I would be easier if you can share geoplugin URL in your question Commented Mar 1, 2019 at 6:33
  • 1
    @ShoaibMunir check my updated question
    – Ajay Patel
    Commented Mar 1, 2019 at 7:00
  • Did you resolve your issue? Commented Mar 4, 2019 at 12:00
  • @ShoaibMunir yes we resolve this issue
    – Ajay Patel
    Commented Mar 5, 2019 at 5:16
  • 1
    we need to verify our domain in geoplugin website.
    – Ajay Patel
    Commented Mar 5, 2019 at 5:50

1 Answer 1

1

You are doing it correctly, the issue that you are facing is may be due to localhost.

Try above code by passing hard codded IP for testing purpose, like this:

$geoplugin = new geoPlugin();
$yourIp = "206.189.146.36";
$geoplugin->locate();
if ($geoplugin->countryCode) {
    $_SESSION['cCode'] = $geoplugin->countryCode;
} else {
    $_SESSION['cCode'] = "US";
}

When you are accessing geoplugin API through URL, then geoplugin is fetching you IP address, but from local host it would fetching 127.0.0.1 or something like this

Hope you find your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.