GEOIP and getting the IP address of a computer?

I want my site to be able to determine the location of computers, so if someone accesses my site from London or Manchester, etc., And so that users from a certain area are displayed depending on the location of their computers. sort of like an online dating site that offers users in your area.

I looked through this GEOIP database, which lists all the cities in the world. But I do not know what to do next? Do I need to look at get ip address scripts that extract and compare information from the GEOip database?

Please someone can point me in the right direction. Thank you

GeoIP Database from: http://dev.maxmind.com/geoip/geolite

+5
source share
7 answers

Try the code below.

$ip = $_SERVER['REMOTE_ADDR']; echo $location = file_get_contents("http://api.hostip.info/country.php?ip=$ip"); 

Do not try to use this in your local host. It will provide ip 127.0.0.1.

 echo $location= file_get_contents("http://api.hostip.info/country.php?ip=12.215.42.19"); //outputs US 
+4
source

PHP has built-in functions for GeoIP

Geoip library

This function can be especially useful since it returns the mainland, country, city, etc. into an array.

geoip_record_by_name

Use $_SERVER['REMOTE_ADDR'] as your ip.

$record = geoip_record_by_name( $_SERVER['REMOTE_ADDR']);

+4
source

It depends on your needs. I would suggest you use a web service, so you don’t have to worry about hosting the database and saving it yourself. All you have to do is parse your visitor's IP address and get the result instantly. I am using the IP2location web service from this site .

+4
source

I used this one in PHP on my website. Very fast and easy to digest (xml, etc.): http://www.geoplugin.com/

+1
source

Please note: this database: In our recent tests, the GeoIP databases tested at 99.8% accurate on a country level, 90% accurate on a state level in the US, and 83% accurate for cities in the US within a 40 kilometer radius.

I would look something like PEAR GeoLite library

A quick snippet of using this library in php:

 $geoip = Net_GeoIP::getInstance(dirname(__FILE__) . '/data/GeoLiteCity.dat'); $ipaddress = '72.30.2.43'; // Yahoo! $location = $geoip->lookupLocation($ipaddress); var_dump($location); 

The result will be displayed, something similar:

 object(Net_GeoIP_Location)[2] protected 'aData' => array 'countryCode' => string 'US' (length=2) 'countryCode3' => string 'USA' (length=3) 'countryName' => string 'United States' (length=13) 'region' => string 'CA' (length=2) 'city' => string 'Sunnyvale' (length=9) 'postalCode' => string '94089' (length=5) 'latitude' => float 37.4249 'longitude' => float -122.0074 'areaCode' => int 408 'dmaCode' => float 807 
+1
source

You probably already have GEOIP as a PECL extension. If you do not, you can simply install it yourself.

If you need something more specific than a country, you will have to pay to get the database, but functions still exist.

0
source

You can use the services https://geoip-db.com . Php JSONP example that returns the location of a given IP address.

 <?php $json = file_get_contents('https://geoip-db.com/jsonp/82.196.6.158'); $data = jsonp_decode($json); print $data->country_code . '<br>'; print $data->country_name . '<br>'; print $data->state . '<br>'; print $data->city . '<br>'; print $data->postal . '<br>'; print $data->latitude . '<br>'; print $data->longitude . '<br>'; print $data->IPv4 . '<br>'; function jsonp_decode($jsonp, $assoc = false) { if($jsonp[0] !== '[' && $jsonp[0] !== '{') { $jsonp = substr($jsonp, strpos($jsonp, '(')); } return json_decode(trim($jsonp,'();'), $assoc); } ?> 
0
source

Source: https://habr.com/ru/post/1443260/


All Articles