[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/pear/Net/ -> GeoIP.php (summary)

+----------------------------------------------------------------------+ | PHP version 5                                                        | +----------------------------------------------------------------------+ | Copyright (C) 2004 MaxMind LLC                                       | +----------------------------------------------------------------------+ | This library is free software; you can redistribute it and/or        | | modify it under the terms of the GNU Lesser General Public           | | License as published by the Free Software Foundation; either         | | version 2.1 of the License, or (at your option) any later version.   | |                                                                      | | This library is distributed in the hope that it will be useful,      | | but WITHOUT ANY WARRANTY; without even the implied warranty of       | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    | | Lesser General Public License for more details.                      | |                                                                      | | You should have received a copy of the GNU Lesser General Public     | | License along with this library; if not, write to the Free Software  | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 | | USA, or view it online at http://www.gnu.org/licenses/lgpl.txt.      | +----------------------------------------------------------------------+ | Authors: Jim Winstead <jimw@apache.org> (original Maxmind version)   | |          Hans Lellelid <hans@xmpl.org>                               | +----------------------------------------------------------------------+

Author: Jim Winstead <jimw@apache.org> (original Maxmind PHP API)
Author: Hans Lellelid <hans@xmpl.org>
License: LGPL http://www.gnu.org/licenses/lgpl.txt
File Size: 904 lines (37 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

Net_GeoIP:: (16 methods):
  __construct()
  getInstance()
  open()
  loadSharedMemory()
  setupSegments()
  close()
  lookupCountryId()
  lookupCountryCode()
  lookupCountryName()
  seekCountry()
  lookupOrg()
  lookupRegion()
  lookupLocation()
  getOrg()
  getRegion()
  getRecord()


Class: Net_GeoIP  - X-Ref

GeoIP class provides an API for performing geo-location lookups based on IP
address.

To use this class you must have a [binary version] GeoIP database. There is
a free GeoIP country database which can be obtained from Maxmind:
{@link http://www.maxmind.com/app/geoip_country}


<b>SIMPLE USE</b>


Create an instance:

<code>
$geoip = Net_GeoIP::getInstance('/path/to/geoipdb.dat', Net_GeoIP::SHARED_MEMORY);
</code>

Depending on which database you are using (free, or one of paid versions)
you must use appropriate lookup method:

<code>
// for free country db:
$country_name = $geoip->lookupCountryName($_SERVER['REMOTE_ADDR']);
$country_code = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);

// for [non-free] region db:
list($ctry_code, $region) = $geoip->lookupRegion($_SERVER['REMOTE_ADDR']);

// for [non-free] city db:
$location = $geoip->lookupLocation($_SERVER['REMOTE_ADDR']);
print "city: " . $location->city . ", " . $location->region;
print "lat: " . $location->latitude . ", long: " . $location->longitude;

// for organization or ISP db:
$org_or_isp_name = $geoip->lookupOrg($_SERVER['REMOTE_ADDR']);
</code>


<b>MULTIPLE INSTANCES</b>


You can have several instances of this class, one for each database file
you are using.  You should use the static getInstance() singleton method
to save on overhead of setting up database segments.  Note that only one
instance is stored per filename, and any flags will be ignored if an
instance already exists for the specifiedfilename.

<b>Special note on using SHARED_MEMORY flag</b>

If you are using SHARED_MEMORY (shmop) you can only use SHARED_MEMORY for
one (1) instance  (i.e. for one database). Any subsequent attempts to
instantiate using SHARED_MEMORY will read the same shared memory block
already initialized, and therefore will cause problems since the expected
database format won't match the database in the shared memory block.

Note that there is no easy way to flag "nice errors" to prevent attempts
to create new instances using SHARED_MEMORY flag and it is also not posible
(in a safe way) to allow new instances to overwrite the shared memory block.

In short, is you are using multiple databses, use the SHARED_MEMORY flag
with care.


<b>LOOKUPS ON HOSTNAMES</b>


Note that this PHP API does NOT support lookups on hostnames.  This is so
that the public API can be kept simple and so that the lookup functions
don't need to try name lookups if IP lookup fails (which would be the only
way to keep the API simple and support name-based lookups).

If you do not know the IP address, you can convert an name to IP very
simply using PHP native functions or other libraries:

<code>
$geoip->lookupCountryName(gethostbyname('www.sunset.se'));
</code>

Or, if you don't know whether an address is a name or ip address, use
application-level logic:

<code>
if (ip2long($ip_or_name) === false) {
$ip = gethostbyname($ip_or_name);
} else {
$ip = $ip_or_name;
}
$ctry = $geoip->lookupCountryName($ip);
</code>

__construct($filename = null, $flags = null)   X-Ref
Construct a Net_GeoIP instance.
You should use the getInstance() method if you plan to use multiple databases or
the same database from several different places in your script.

param: string $filename Path to binary geoip database.
param: int    $flags    Flags

getInstance($filename = null, $flags = null)   X-Ref
Singleton method, use this to get an instance and avoid re-parsing the db.

Unique instances are instantiated based on the filename of the db. The flags
are ignored -- in that requests to for instance with same filename but different
flags will return the already-instantiated instance.  For example:
<code>
// create new instance with memory_cache enabled
$geoip = Net_GeoIP::getInstance('C:\mydb.dat', Net_GeoIP::MEMORY_CACHE);
....

// later in code, request instance with no flags specified.
$geoip = Net_GeoIP::getInstance('C:\mydb.dat');

// Normally this means no MEMORY_CACHE but since an instance
// with memory cache enabled has already been created for 'C:\mydb.dat', the
// existing instance (with memory cache) will be returned.
</code>

NOTE: You can only use SHARED_MEMORY flag for one instance!  Any subsquent instances
that attempt to use the SHARED_MEMORY will use the *same* shared memory, which will break
your script.

param: string $filename Filename
param: int    $flags    Flags that control class behavior.
return: Net_GeoIP

open($filename, $flags = null)   X-Ref
Opens geoip database at filename and with specified flags.

param: string $filename File to open
param: int    $flags    Flags
return: void

loadSharedMemory($filename)   X-Ref
Loads the database file into shared memory.

param: string $filename Path to database file to read into shared memory.
return: void

setupSegments()   X-Ref
Parses the database file to determine what kind of database is being used and setup
segment sizes and start points that will be used by the seek*() methods later.

return: void

close()   X-Ref
Closes the geoip database.

return: int Status of close command.

lookupCountryId($addr)   X-Ref
Get the country index.

This method is called by the lookupCountryCode() and lookupCountryName()
methods.  It lookups up the index ('id') for the country which is the key
for the code and name.

param: string $addr IP address (hostname not allowed)
return: string ID for the country

lookupCountryCode($addr)   X-Ref
Returns 2-letter country code (e.g. 'CA') for specified IP address.
Use this method if you have a Country database.

param: string $addr IP address (hostname not allowed).
return: string 2-letter country code

lookupCountryName($addr)   X-Ref
Returns full country name for specified IP address.
Use this method if you have a Country database.

param: string $addr IP address (hostname not allowed).
return: string Country name

seekCountry($ipnum)   X-Ref
Using the record length and appropriate start points, seek to the country that corresponds
to the converted IP address integer.

param: int $ipnum Result of ip2long() conversion.
return: int Offset of start of record.

lookupOrg($addr)   X-Ref
Lookup the organization (or ISP) for given IP address.
Use this method if you have an Organization/ISP database.

param: string $addr IP address (hostname not allowed).
return: string The organization

lookupRegion($addr)   X-Ref
Lookup the region for given IP address.
Use this method if you have a Region database.

param: string $addr IP address (hostname not allowed).
return: array Array containing country code and region: array($country_code, $region)

lookupLocation($addr)   X-Ref
Lookup the location record for given IP address.
Use this method if you have a City database.

param: string $addr IP address (hostname not allowed).
return: Net_GeoIP_Location The full location record.

getOrg($ipnum)   X-Ref
Seek and return organization (or ISP) name for converted IP addr.

param: int $ipnum Converted IP address.
return: string The organization

getRegion($ipnum)   X-Ref
Seek and return the region info (array containing country code and region name) for converted IP addr.

param: int $ipnum Converted IP address.
return: array Array containing country code and region: array($country_code, $region)

getRecord($ipnum)   X-Ref
Seek and populate Net_GeoIP_Location object for converted IP addr.
Note: this

param: int $ipnum Converted IP address.
return: Net_GeoIP_Location



Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1