4

I have access to the GeoIP.dat file through my current host but every IP I enter is empty. Any idea what might be wrong or a way to troubleshoot it? I have php errors on but I don't see anything wrong.

Here is my current script:

<?php

include('geoip.inc');
include('geoipcity.inc');
include('geoipregionvars.php');


$gi = geoip_open("GeoIP.dat", GEOIP_STANDARD);

$rsGeoData = geoip_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);

echo("<pre>");
print_r($rsGeoData);
echo("</pre>");

geoip_close($gi);

?>

The result always looks like this:

geoiprecord Object
(
[country_code] => 
[country_code3] => 
[country_name] => 
[region] => 
[city] => 
[postal_code] => 
[latitude] => -180
[longitude] => -180
[area_code] => 
[dma_code] => 
[metro_code] => 
[continent_code] => --
)
Paul
  • 11,671
  • 32
  • 91
  • 143
  • 1
    depending on how your webserver is set up, $_SERVER['REMOTE_ADDR'] may not contain the client's ip address. Try echoing $_SERVER['REMOTE_ADDR'] to ensure the correct IP is there. – 0x6A75616E Dec 12 '11 at 21:01
  • I thought of that and have verified it's the correct IP. – Paul Dec 12 '11 at 21:04
  • What does `var_dump($_SERVER['REMOTE_ADDR']);` output? Where is `geoip_open` and `geoip_record_by_addr` defined? What does `GeoIP.dat` specifically contains, both which format by what standard and what is the actual data? – hakre Dec 12 '11 at 21:04
  • @Paul, try var_dump( is_readable("GeoIP.dat") ); to make sure you have read access to the dat file and to make sure that's the right path to it. – 0x6A75616E Dec 12 '11 at 21:05
  • juand: Looks like it's readable: boolean true (I had initially verified I could read the file) This is driving me crazy...everything seems to be correct – Paul Dec 12 '11 at 21:06
  • hmm.. I'm somewhat familiar with MaxMind and I know their city-level db is not free but you're trying to use their free country level one right? if so, is it possible that geoipcity.inc is overriding the function geoip_record_by_addr()? E.g. maybe that function is defined in geoip.inc does that make sense? try commenting out include('geoipcity.inc'); – 0x6A75616E Dec 12 '11 at 21:11
  • According to their readme at http://geolite.maxmind.com/download/geoip/api/php/README you're doing it right, except for the other 2 includes you have there. I'd try putting a full path to GeoIP.dat just to test and leaving just geoip.inc included.. – 0x6A75616E Dec 12 '11 at 21:15
  • juand: The GeoIP.dat file on my host server is the full DB. I tried commenting out geoipcity.inc but it gives the error: Fatal error: Call to undefined function geoip_record_by_addr() – Paul Dec 12 '11 at 21:22

1 Answers1

2

Make sure you are using the correct version of GeoIP. I have this code and it is working properly. Notice the different database file:

include("geoipcity.inc");
include("geoipregionvars.php");

$gi = geoip_open('geolitecity.dat', GEOIP_STANDARD);
$record = geoip_record_by_addr($gi, ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') ? '83.238.225.249' : $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
Pateman
  • 2,727
  • 3
  • 28
  • 43