geoip_functions.php

Summary
geoip_functions.php
DescriptionThis file holds all extended functions which belongs to IP and GEOIP handling for use in Website Baker modules.
Introduction
How to use the GeoIP-module for your own Website Baker ProjectsYou have to include (per require_once() ) this file to use the geoip-module.
If GeoIP-Module is required:Example on how to load the GeoIP-Module, in case its required.
If GeoIP-module is optional:Example on how to load the GeoIP-Module, in case its optional.
Functions
GeoIP-Functions
geoip_open_dbOpens GeoIP-database for use.
GEOIP_DATABASE_LOADEDThe Constant GEOIP_DATABASE_LOADED will be defined when the GeoIP-Database was opened successfully.
geoip_close_dbCloses GeoIP-database after use.
geoip_flag_htmlReturns a piece of HTML-code to display a flag.
geoip_flag_linkReturns an absolute URL to a flag-image.
geoip_best_ipFetches best matching IP for actual connection.
geoip_list_ipFetches user-ip and proxy-ip, if available.
geoip_fetch_hostnameFetches hostname for given IP.
geoip_list_hostnameFetches and checks hostname for a given IP.
geoip_is_proxyChecks if the actual user ( $_SERVER['REMOTE_ADDR'] ) is behind a proxy.
geoip_in_bogons_listChecks if the given IP is in an unallocated or private address range.
geoip_in_drop_listChecks if the given IP is in spamhaus' drop-list.
geoip_country_code_by_addr_Fetches country-code by ip.
geoip_country_name_by_addr_Fetches country-name by ip.
geoip_whois_link_Create a clickable link for a WhoIs-Query.

Description

This file holds all extended functions which belongs to IP and GEOIP handling for use in Website Baker modules.

Introduction

Summary
How to use the GeoIP-module for your own Website Baker ProjectsYou have to include (per require_once() ) this file to use the geoip-module.
If GeoIP-Module is required:Example on how to load the GeoIP-Module, in case its required.
If GeoIP-module is optional:Example on how to load the GeoIP-Module, in case its optional.

How to use the GeoIP-module for your own Website Baker Projects

You have to include (per require_once() ) this file to use the geoip-module.

If GeoIP-Module is required:

Example on how to load the GeoIP-Module, in case its required.

// load geoip-functions
if(file_exists(WB_PATH.'/modules/geoip/geoip_functions.php'))
   require_once(WB_PATH.'/modules/geoip/geoip_functions.php');
else
   exit('GeoIP-module not installed!');

//...

// use geoip-functions
$gi = geoip_open_db(); // opens GeoIP database
if($gi===FALSE)
   exit('Failed to open GeoIP-database!');
$ip = geoip_best_ip();
echo 'You are from '.geoip_country_name_by_addr($gi, $ip);
geoip_close($gi);

If GeoIP-module is optional:

Example on how to load the GeoIP-Module, in case its optional.

// load geoip-functions, if available
if(file_exists(WB_PATH.'/modules/geoip/geoip_functions.php'))
   require_once(WB_PATH.'/modules/geoip/geoip_functions.php');

if(function_exists('geoip_open_db'))
   $gi = geoip_open_db(); // $gi will be FALSE if this fails
else
   $gi = FALSE;

// Now you can check against $gi or defined('GEOIP_DATABASE_LOADED')
// to see if geoip-database is ready to use
// if($gi) { // ready to use } else { // not available }
// if(defined('GEOIP_DATABASE_LOADED')) { // ready to use }
// else { // not available }

//...

if($gi)
   $ip = geoip_best_ip();
else
   $ip = $_SERVER['REMOTE_ADDR'];

//...

if($gi) {
   echo '<strong>You are from '.geoip_country_name_by_addr($gi, $ip).'</strong></br >';
} else {
   echo 'Your IP-Address is: '.$ip.'<br />';
}

if($gi)
   geoip_close($gi);

Functions

Summary
GeoIP-Functions
geoip_open_dbOpens GeoIP-database for use.
GEOIP_DATABASE_LOADEDThe Constant GEOIP_DATABASE_LOADED will be defined when the GeoIP-Database was opened successfully.
geoip_close_dbCloses GeoIP-database after use.
geoip_flag_htmlReturns a piece of HTML-code to display a flag.
geoip_flag_linkReturns an absolute URL to a flag-image.
geoip_best_ipFetches best matching IP for actual connection.
geoip_list_ipFetches user-ip and proxy-ip, if available.
geoip_fetch_hostnameFetches hostname for given IP.
geoip_list_hostnameFetches and checks hostname for a given IP.
geoip_is_proxyChecks if the actual user ( $_SERVER['REMOTE_ADDR'] ) is behind a proxy.
geoip_in_bogons_listChecks if the given IP is in an unallocated or private address range.
geoip_in_drop_listChecks if the given IP is in spamhaus' drop-list.
geoip_country_code_by_addr_Fetches country-code by ip.
geoip_country_name_by_addr_Fetches country-name by ip.
geoip_whois_link_Create a clickable link for a WhoIs-Query.

GeoIP-Functions

geoip_open_db

function geoip_open_db($use_cache = FALSE)

Opens GeoIP-database for use.

Prototype

handle geoip_open_db( bool $use_cache )

Parameters

use_cache(bool) Whether to cache the database in memory (TRUE), or not (FALSE).

Returns

A GeoIP Database-Handle.

In case of error, the function returns FALSE.

Note

You should use geoip_open_db(TRUE) if you call geoip functions more than once.

GEOIP_DATABASE_LOADED

The Constant GEOIP_DATABASE_LOADED will be defined when the GeoIP-Database was opened successfully.

geoip_close_db

function geoip_close_db($gi)

Closes GeoIP-database after use.

Prototype

void geoip_close_db( handle $gi )

Parameters

gi(handle) The handle returned from geoip_open_db().

Returns

nothing.

geoip_flag_html

function geoip_flag_html($gi,
$src)

Returns a piece of HTML-code to display a flag.

Prototype

string geoip_flag_html( handle $gi, string source )

Parameters

gi(handle) The handle returned from geoip_open_db().
source(string) An IP-Address (e.g.  '100.20.101.90' ), or a Country-Code (e.g.  'DE' ), or a Country-Name (e.g.  'Germany' ).

Returns

A string, containing a piece of code to display an image from the supplied flags-collection.

For an unkown IP or Country-Name or -Code, the special (empty) __.png-flag will be returned.

Example

echo geoip_flag_html($gi, '87.90.102.80');

may output

<img class="geoip_worldflag"
     src="http://www.example.org/wb/modules/geoip/worldflags/de.png"
     alt="Germany" title="Germany" />

geoip_flag_link

function geoip_flag_link($gi,
$src)

Returns an absolute URL to a flag-image.

Prototype

string geoip_flag_link( handle $gi, string source )

Parameters

gi(handle) The handle returned from geoip_open_db().
source(string) An IP-Address (e.g.  '100.20.101.90' ), or a Country-Code (e.g.  'DE' ), or a Country-Name (e.g.  'Germany' ).

Returns

A string, containing an absolute URL to an image from the supplied flags-collection.

For an unkown IP or Country-Name or -Code, a URL to the special (empty) __.png-flag will returned.

Example

echo geoip_flag_link($gi, '87.90.102.80');

may output

http://www.example.org/wb/modules/geoip/worldflags/de.png

geoip_best_ip

function geoip_best_ip()

Fetches best matching IP for actual connection.

The "best matching" IP is either the User-IP, or, if the user is hidden behind a proxy, that Proxy-IP.

Prototype

string geoip_best_ip( void )

Parameters

Uses

$_SERVER['REMOTE_ADDR'] and miscellaneous other $_SERVER[] variables

Returns

A string with the fetched IP.

There is no guarantee that the fetched ip is really "real"; users may forge it.  But this is better than nothing

Note

There is no need to check the returned IP against geoip_in_bogons_list(), since this is done inside this function already.

geoip_list_ip

function geoip_list_ip()

Fetches user-ip and proxy-ip, if available.

Prototype

array geoip_list_ip( void )

Parameters

Uses

$_SERVER['REMOTE_ADDR'] and miscellaneous other $_SERVER[] variables

Returns

An array with User-IP and Proxy-IP (array(0=>'100.10.20.3', 1=>'200.20.10.20') ).  User-IP or Proxy-IP may be FALSE

There is no guarantee that the fetched ip is really "real"; users may forge it.  But this is better than nothing

Example

list($real_ip, $proxy_ip) = geoip_list_ip($_SERVER['REMOTE_ADDR']);
$real_ip │ $proxy_ip │ meaning
─────────┼───────────┼──────────────────────────────
100.1.2.3│   FALSE   │ real ip, no proxy ip
 FALSE   │ 200.1.2.3 │ real ip unknown, proxy ip
100.1.2.3│ 200.3.2.1 │ real ip, proxy ip

Note

There is no need to check the returned User-IP (if any) against geoip_in_bogons_list(), since this is done inside this function already.

geoip_fetch_hostname

function geoip_fetch_hostname($ip)

Fetches hostname for given IP.

Prototype

string geoip_fetch_hostname( string $ip )

Parameters

ip(string) An IP-Address (e.g.  '100.10.20.2' ).

Returns

The resolved Hostname, or the given IP if it isn't resolvable or if the hostname is faked.

Examples

echo geoip_fetch_hostname('87.20.30.40');

host40-30-dynamic.20-87-r.retail.telecomitalia.it

echo geoip_fetch_hostname('100.20.30.40');

100.20.30.40

geoip_list_hostname

function geoip_list_hostname($ip)

Fetches and checks hostname for a given IP.

Prototype

array geoip_list_hostname( string $ip )

Parameters

ip(string) An IP-Address (e.g.  '100.10.20.2' ).

Returns

An array which will contain the hostname and the result of the hostname-check as int-value.  array(0=>'faked.example.com', 1=>0)

Example

list($hostname, $result) = geoip_list_hostname($_SERVER['REMOTE_ADDR']);
    $hostname     │result│ meaning
──────────────────┼──────┼───────────────────────────────────────────
 faked.google.com │   0  │ hostname faked
dd-112.example.org│   1  │ ok
     200.2.3.1    │   2  │ no hostname
xx3323.example.com│   3  │ hostname has no ip (don't care about this)

Just consider $result >0 as 'real' hostnames.

if($result==0) { // faked }
else { // OK }

geoip_is_proxy

function geoip_is_proxy()

Checks if the actual user ( $_SERVER['REMOTE_ADDR'] ) is behind a proxy.

Prototype

bool geoip_is_proxy( void )

Parameters

Uses

Miscellaneous $_SERVER[]; variables

Returns

TRUE if the user is behind a proxy.  FALSE if there is no evidence for a proxy.

geoip_in_bogons_list

function geoip_in_bogons_list($ip)

Checks if the given IP is in an unallocated or private address range.

Prototype

bool geoip_in_bogons_list( string $ip )

Parameters

ip(string) An IP-Address (e.g.  '100.10.20.2' ).

Returns

TRUE if the IP is in the bogons-list, FALSE if it is not.

Examples

$ip = '127.0.0.1';
if(geoip_in_bogons_list($ip)) {
  echo 'private address -- useless';
} else {
  echo 'OK';
}

Will output private address -- useless for ip 127.0.0.1

geoip_in_drop_list

function geoip_in_drop_list($ip)

Checks if the given IP is in spamhaus' drop-list.

DROP (Don't Route Or Peer) is an advisory "drop all traffic" list, consisting of stolen 'zombie' netblocks and netblocks controlled entirely by professional spammers.

Prototype

bool geoip_in_drop_list( string $ip )

Parameters

ip(string) An IP-Address (e.g.  '100.10.20.2' ).

Returns

TRUE if the IP is in drop-list, FALSE if it is not.

geoip_country_code_by_addr_

Fetches country-code by ip.

See geoip_country_code_by_addr

geoip_country_name_by_addr_

Fetches country-name by ip.

See geoip_country_name_by_addr

geoip_whois_link_

Create a clickable link for a WhoIs-Query.

See Usage of phpwhois and geoip_whois_link

function geoip_open_db($use_cache = FALSE)
Opens GeoIP-database for use.
function geoip_close_db($gi)
Closes GeoIP-database after use.
function geoip_flag_html($gi,
$src)
Returns a piece of HTML-code to display a flag.
function geoip_flag_link($gi,
$src)
Returns an absolute URL to a flag-image.
function geoip_best_ip()
Fetches best matching IP for actual connection.
function geoip_list_ip()
Fetches user-ip and proxy-ip, if available.
function geoip_fetch_hostname($ip)
Fetches hostname for given IP.
function geoip_list_hostname($ip)
Fetches and checks hostname for a given IP.
function geoip_is_proxy()
Checks if the actual user ( $_SERVER['REMOTE_ADDR'] ) is behind a proxy.
function geoip_in_bogons_list($ip)
Checks if the given IP is in an unallocated or private address range.
function geoip_in_drop_list($ip)
Checks if the given IP is in spamhaus' drop-list.
function geoip_country_code_by_addr($gi,
$addr)
Fetches country-code
function geoip_country_name_by_addr($gi,
$addr)
Fetches country-name
You have to include (per require_once() ) this file to use the WhoIs-Functions.
function geoip_whois_link($ip_addr)
Create a clickable link for a WhoIs-Query.
Close