maxmind / GeoIP2-php

PHP API for GeoIP2 webservice client and database reader
https://maxmind.github.io/GeoIP2-php/
Apache License 2.0
2.33k stars 276 forks source link

"The address 192.168.1.1 is not in the database" #103

Closed emresaracoglu closed 6 years ago

emresaracoglu commented 6 years ago

Hello,

I get this error because of my IP address. Access from different IP addresses but I get this error with my IP address.

what could be the reason?

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    $reader = new Reader('/home/USERNAME/public_html/sistem/GeoLite2-City.mmdb');
    $record = $reader->city($ip);

Fatal error: Uncaught GeoIp2\Exception\AddressNotFoundException: The address 192.168.1.1 is not in the database. in /home/USERNAME/vendor/geoip2/geoip2/src/Database/Reader.php:244 Stack trace: #0 /home/USERNAME/vendor/geoip2/geoip2/src/Database/Reader.php(215): GeoIp2\Database\Reader->getRecord('City', 'City', '192.168.1.1') #1 /home/USERNAME/vendor/geoip2/geoip2/src/Database/Reader.php(71): GeoIp2\Database\Reader->modelFor('City', 'City', '192.168.1.1') #2 /home/USERNAME/public_html/AppConfig.php(120): GeoIp2\Database\Reader->city('192.168.1.1') #3 /home/USERNAME/public_html/theme/v10/Hosting.php(10): include('/home/USERNAME/...') #4 /home/USERNAME/public_html/index.php(112): include('/home/USERNAME/...') #5 {main} thrown in /home/USERNAME/vendor/geoip2/geoip2/src/Database/Reader.php on line 244

emresaracoglu commented 6 years ago

I solved this method:

$externalContent = file_get_contents('http://checkip.dyndns.com/');
preg_match('/Current IP Address: \[?([:.0-9a-fA-F]+)\]?/', $externalContent, $m);
$externalIp = $m[1];

But what is the real solution to this?

klp2 commented 6 years ago

It looks like you end up setting $ip to 192.168.1.1 which is a reserved IP and isn't expected to be included in any of the GeoIP2 databases. You probably want to look through that conditional to figure out where the IP is coming from. This isn't related to the API itself though.

ramsampathi commented 5 years ago

externalIp @emresaracoglu where should be place this code in Reader.php file ?

thanks