文字

GeoIP 函数

Table of Contents

  • geoip_asnum_by_name — Get the Autonomous System Numbers (ASN)
  • geoip_continent_code_by_name — Get the two letter continent code
  • geoip_country_code_by_name — Get the two letter country code
  • geoip_country_code3_by_name — Get the three letter country code
  • geoip_country_name_by_name — Get the full country name
  • geoip_database_info — Get GeoIP Database information
  • geoip_db_avail — Determine if GeoIP Database is available
  • geoip_db_filename — Returns the filename of the corresponding GeoIP Database
  • geoip_db_get_all_info — Returns detailed information about all GeoIP database types
  • geoip_domain_by_name — Get the second level domain name
  • geoip_id_by_name — Get the Internet connection type
  • geoip_isp_by_name — Get the Internet Service Provider (ISP) name
  • geoip_netspeedcell_by_name — Get the Internet connection speed
  • geoip_org_by_name — Get the organization name
  • geoip_record_by_name — Returns the detailed City information found in the GeoIP Database
  • geoip_region_by_name — Get the country code and region
  • geoip_region_name_by_code — Returns the region name for some country and region code combo
  • geoip_setup_custom_directory — Set a custom directory for the GeoIP database.
  • geoip_time_zone_by_country_and_region — Returns the time zone for some country and region code combo

用户评论:

[#1] nospam dot guilhem at nospam dot answeb dot net [2011-04-05 02:26:27]

Hello,

While trying to update the GeoIP.dat file under debian, I've found out that automatic updates via the geoipupdate (in geoip-bin package) is not available for the free edition. Here is a one-liner script that will do it. You can put it in a crontab, it will download the .dat file only if it has change :

cd /usr/share/GeoIP && wget GEOIP_URL -N && cat GeoIP.dat.gz | gzip -d > GeoIP.dat

Note : GEOIP_URL is to be found on the page http://www.maxmind.com/app/geoip_country, under the Binary Format section (The php note system won't allow me to put the full URL here

Crontab line could be :

@weekly cd /usr/share/GeoIP && (..)

[#2] nandox7 at myrealbox dot com [2008-12-26 18:49:18]

The GeoLiteCity free db works and can resolve city names and other informations.

Make sure the instructions are followed regarding it's installation.

http://www.maxmind.com/app/installation?city=1

At step 2 the decompress file must be moved and renamed.
The files needs to be called GeoIPCity.dat if not the mod_geoip will not find it.

[#3] thomas {AT} thomasfischer {D0T} BIZ [2008-04-07 17:04:40]

you can use this function to determine the continent of a country:

function getContinentFromCountry($country)
{
 $data = array ("AF"=> "AS","AX"=>"EU","AL"=>"EU", "DZ"=>"AF","AS"=>"OC", "AD"=>"EU","AO"=> "AF" ........);
 return $data[$country];
}

unfortunately this comment's size is restricted, so you will have the rest by yourself

for example:
getContinentFromCountry("DE") will result in "EU"

this data is based on 
wikipedia's
List_of_countries_by_continent_(data_file)

[#4] kapouer at melix dot org [2008-03-01 07:26:57]

For Debian etch users : libgeoip is in version 1.4 so get the source from lenny (apt-get build-dep libgeoip;apt-get source libgeoip), compile it on etch (dpkg-buildpackage -rfakeroot in source folder) and install resulting libgeoip1 and libgeo-dev packages (dpkg -i)... Together with php5-dev this package is easy to install (phpize5;./configure --enable-geoip;make;make install)... Then check your install in php with :
<?php
    
echo geoip_database_info(GEOIP_COUNTRY_EDITION);
    echo 
$_SERVER['REMOTE_ADDR'];
    echo 
geoip_country_code3_by_name($_SERVER['REMOTE_ADDR']);
?>


please note free .dat is in /usr/share/GeoIP/GeoIP.dat
Use geoip-bin package to schedule automatic updates (configure /etc/geoip.default.conf and copy to /etc/geoip.conf, then add a crontab to make the updates...)
hope this helps

[#5] php dot comment dot addition at gmail dot com [2007-06-10 20:57:28]

Just to clarify the other comments, the maxmind post seems to be out of date / incorrect.  

This module fails to work (ie: resolve cities) with the GeoLite City database from here:
http://www.maxmind.com/app/geolitecity

The error occurs when calling the function geoip_record_by_name(), which states incorrectly that the required database is not available.  I have tested it with the very latest database, for which geoip_database_info() returns  the version:
GEO-533LITE 20070601 Build 1 Copyright (c) 2007 MaxMind LLC All Rights Reserved

It does, however, resolve countries OK via the GeoLite Country database, which you can grab here: http://www.maxmind.com/app/geoip_country
I tested with the version:

GEO-106FREE 20070601 Build 1 Copyright (c) 2007 MaxMind LLC All Rights Reserved

[#6] tjmather at maxmind dot com [2007-02-26 20:09:51]

The geoip_country_*_by_name functions only work with GeoIP Country.  The geoip_record_by_name function works with GeoIP City.  geoip_region_by_name works with GeoIP Region.  So the API should work with all of the GeoIP databases, as long as you call the correct function.

[#7] [2007-01-26 17:23:44]

I couldn't get it to work with the DMA code database (product ID 137).  It seems to only work with the country database.  The documentation should contain a warning that this code (as of 0.2.0) is incomplete and that Net_GeoIP should be used for non-country databases.

[#8] judas dot iscariote at gmail dot com [2006-11-16 19:59:49]

If you want to use this extension in SUSE/openSUSE, it is available here:

http://repos.opensuse.org/server:/php:/extensions/

[#9] Christian [2006-09-13 05:45:11]

Both, Ubuntu and Debian itself make usage of version 1.3.x of the c libraries, so installation can't be done with the debian packages, because configure stops with:
 
== configure: error: You need version 1.4.0 or higher of the C API ==

so you must download it from maxmind.

[#10] Matt [2006-09-08 13:34:34]

If you are a Debian or Ubuntu user, instead of building it from source, you can: 
aptitude install libgeoip-dev libgeoip1

上一篇: 下一篇: