| geoip_functions.php | |
| Description | This 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 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. |
| If GeoIP-module is optional: | Example on how to load the GeoIP-Module, in case its optional. |
| Functions | |
| GeoIP-Functions | |
| geoip_open_db | Opens GeoIP-database for use. |
| GEOIP_DATABASE_LOADED | The Constant GEOIP_DATABASE_LOADED will be defined when the GeoIP-Database was opened successfully. |
| geoip_close_db | Closes GeoIP-database after use. |
| geoip_flag_html | Returns a piece of HTML-code to display a flag. |
| geoip_flag_link | Returns an absolute URL to a flag-image. |
| geoip_best_ip | Fetches best matching IP for actual connection. |
| geoip_list_ip | Fetches user-ip and proxy-ip, if available. |
| geoip_fetch_hostname | Fetches hostname for given IP. |
| geoip_list_hostname | Fetches and checks hostname for a given IP. |
| geoip_is_proxy | Checks if the actual user ( $_SERVER['REMOTE_ADDR'] ) is behind a proxy. |
| geoip_in_bogons_list | Checks if the given IP is in an unallocated or private address range. |
| geoip_in_drop_list | Checks 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. |
| 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. |
| If GeoIP-module is optional: | Example on how to load the GeoIP-Module, in case its optional. |
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);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);| GeoIP-Functions | |
| geoip_open_db | Opens GeoIP-database for use. |
| GEOIP_DATABASE_LOADED | The Constant GEOIP_DATABASE_LOADED will be defined when the GeoIP-Database was opened successfully. |
| geoip_close_db | Closes GeoIP-database after use. |
| geoip_flag_html | Returns a piece of HTML-code to display a flag. |
| geoip_flag_link | Returns an absolute URL to a flag-image. |
| geoip_best_ip | Fetches best matching IP for actual connection. |
| geoip_list_ip | Fetches user-ip and proxy-ip, if available. |
| geoip_fetch_hostname | Fetches hostname for given IP. |
| geoip_list_hostname | Fetches and checks hostname for a given IP. |
| geoip_is_proxy | Checks if the actual user ( $_SERVER['REMOTE_ADDR'] ) is behind a proxy. |
| geoip_in_bogons_list | Checks if the given IP is in an unallocated or private address range. |
| geoip_in_drop_list | Checks 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. |
function geoip_open_db( $use_cache = FALSE )
Opens GeoIP-database for use.
handle geoip_open_db( bool $use_cache )
| use_cache | (bool) Whether to cache the database in memory (TRUE), or not (FALSE). |
A GeoIP Database-Handle.
In case of error, the function returns FALSE.
You should use geoip_open_db(TRUE) if you call geoip functions more than once.
function geoip_flag_html( $gi, $src )
Returns a piece of HTML-code to display a flag.
string geoip_flag_html( handle $gi, string source )
| 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' ). |
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.
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" />
function geoip_flag_link( $gi, $src )
Returns an absolute URL to a flag-image.
string geoip_flag_link( handle $gi, string source )
| 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' ). |
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.
echo geoip_flag_link($gi, '87.90.102.80');
may output
http://www.example.org/wb/modules/geoip/worldflags/de.png
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.
string geoip_best_ip( void )
$_SERVER['REMOTE_ADDR'] and miscellaneous other $_SERVER[] variables
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
There is no need to check the returned IP against geoip_in_bogons_list(), since this is done inside this function already.
function geoip_list_ip()
Fetches user-ip and proxy-ip, if available.
array geoip_list_ip( void )
$_SERVER['REMOTE_ADDR'] and miscellaneous other $_SERVER[] variables
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
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
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.
function geoip_fetch_hostname( $ip )
Fetches hostname for given IP.
string geoip_fetch_hostname( string $ip )
| ip | (string) An IP-Address (e.g. '100.10.20.2' ). |
The resolved Hostname, or the given IP if it isn't resolvable or if the hostname is faked.
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
function geoip_list_hostname( $ip )
Fetches and checks hostname for a given IP.
array geoip_list_hostname( string $ip )
| ip | (string) An IP-Address (e.g. '100.10.20.2' ). |
An array which will contain the hostname and the result of the hostname-check as int-value. array(0=>'faked.example.com', 1=>0)
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 }
function geoip_in_bogons_list( $ip )
Checks if the given IP is in an unallocated or private address range.
bool geoip_in_bogons_list( string $ip )
| ip | (string) An IP-Address (e.g. '100.10.20.2' ). |
TRUE if the IP is in the bogons-list, FALSE if it is not.
$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
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.
bool geoip_in_drop_list( string $ip )
| ip | (string) An IP-Address (e.g. '100.10.20.2' ). |
TRUE if the IP is in drop-list, FALSE if it is not.
Create a clickable link for a WhoIs-Query.
See Usage of phpwhois and geoip_whois_link
Opens GeoIP-database for use.
function geoip_open_db( $use_cache = FALSE )
Closes GeoIP-database after use.
function geoip_close_db( $gi )
Returns a piece of HTML-code to display a flag.
function geoip_flag_html( $gi, $src )
Returns an absolute URL to a flag-image.
function geoip_flag_link( $gi, $src )
Fetches best matching IP for actual connection.
function geoip_best_ip()
Fetches user-ip and proxy-ip, if available.
function geoip_list_ip()
Fetches hostname for given IP.
function geoip_fetch_hostname( $ip )
Fetches and checks hostname for a given IP.
function geoip_list_hostname( $ip )
Checks if the actual user ( $_SERVER['REMOTE_ADDR'] ) is behind a proxy.
function geoip_is_proxy()
Checks if the given IP is in an unallocated or private address range.
function geoip_in_bogons_list( $ip )
Checks if the given IP is in spamhaus' drop-list.
function geoip_in_drop_list( $ip )
Fetches country-code
function geoip_country_code_by_addr( $gi, $addr )
Fetches country-name
function geoip_country_name_by_addr( $gi, $addr )
Create a clickable link for a WhoIs-Query.
function geoip_whois_link( $ip_addr )