user location by IP

Asked

Viewed 1,708 times

3

I would like to know a php library that would help me with the location of the user access by IP, well I found one on the internet and even worked well (did not show exactly the location) only a 50 km difference but at least can to know the access city! up to here blz, however this application uses the site http://www.geoplugin.net/ To get the information, if, I load the page bringing the ip, shows the data perfectly but if I put to bring the information at the time when my Ajax is storing the informed in the bank, he error! does not load the link to bring the ip info!

Does anyone know any other method? library or article to help me ? or better someone knows how to explain why php with ajax does not load the link to the variables by?

echo "Geolocation results for {$geoplugin->ip}: <br />\n".
"City: {$geoplugin->city} <br />\n".
"Region: {$geoplugin->region} <br />\n".
"Area Code: {$geoplugin->areaCode} <br />\n".
"DMA Code: {$geoplugin->dmaCode} <br />\n".
"Country Name: {$geoplugin->countryName} <br />\n".
"Country Code: {$geoplugin->countryCode} <br />\n".
"Longitude: {$geoplugin->longitude} <br />\n".
"Latitude: {$geoplugin->latitude} <br />\n".
"Currency Code: {$geoplugin->currencyCode} <br />\n".
"Currency Symbol: {$geoplugin->currencySymbol} <br />\n".
"Exchange Rate: {$geoplugin->currencyConverter} <br />\n";

the geoplugin class already charges ip altomatically then need not create variable for that, only ask the infomraçoes

  • 1

    The only way in the front end, with javascript (Ajax) to get data from a site that can another domain, is if it provides data in JSONP format.

  • 1

    The site can also "declare" in response to the request that accepts this type of request. But it seems that is not your case. Still, if possible, put the code.

  • But what doesn’t work is the ajax request? Or the server side, php?

  • Miguel, I think it is something on the server side, because when I try to load the page ip.php , everything works perfect, but when I use ajax, the page ip.php should load the information of ip and put the variables right away in the bank but it does not do this with ajax, bad mood, I think I’ll try to bring the data via json to see if right, I’ll try to post the scrip for you to see

  • Any error message on the console to confirm if it is a CORS protection issue?

  • good people, I could not solve the problem, the solution was: as soon as the user loads page, in two inputs Hidden loads the information in this case, and then I take this information with ajax

  • 1

    I have already used this configurable Bundle by Composer: https://github.com/aferrandini/Maxmind-GeoIp

  • 1

    Friend, I use the IP-API which is a great library that returns in JSON, CSV, XML, accepts AJAX requests, and accepts up to 150 requests per minute for the same IP. I hope I’ve helped!

Show 3 more comments

1 answer

3


Have you tried this way? with Geolocation.... in JS

/*início código de geolocalização*/
function GeoLocalizacao() {
    if (navigator.geolocation){//verificando se há suporte para API de     Geolocalização
    navigator.geolocation.getCurrentPosition(locSucesso, erro);
    } else {
       $('#status').text('Seu browser não suporta geolocalização!');
    }
}

If you are ok.. just show the data on the map

function locSucesso(position) {
var latlngGeo = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); //pegando localização do usuário
var myOptions = {//opções do mapa
    zoom: 15, //configuração da proximidade de visualização do mapa quando iniciado
    mapTypeId: google.maps.MapTypeId.ROADMAP, //tipo do mapa (ROADMAP --> normal, default 2D map)
    center: latlngGeo
};

map = new google.maps.Map(document.getElementById("mapa"), myOptions);
geocoder = new google.maps.Geocoder();

marker = new google.maps.Marker({
    map: map,
    draggable: true,
    title:"Você está aqui!" //texto quando usuário passar mouse por cima do marcador
});
marker.setPosition(latlngGeo);

var infowindow = new google.maps.InfoWindow({
    content: 'Você está aqui!' //mostrar texto quando usuário clicar no marcador
});

... I tested by sending the data through a form.. ie in my view I created a form with fields Hidden and with the result of geolocation I put the values lat and long (through jQuery) in the form... and with this can send the form via ajax(json) to the controller and treat them for sending in the bank.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.