CityHost.UA
Help and support

How to use the GeoIP module?

The GeoIP module library for apache, nginx and PHP (for all versions that support it) is installed on the hosting servers.
GeoIP library - allows you to get the region to which the IP address from which the request is sent belongs.

Our servers process 4 headers:

 GEOIP_ADDR - IP адреса, з якого відправляється запит; GEOIP_CONTINENT_CODE - код континенту (Європа, Азія, Америка і т.д.); GEOIP_COUNTRY_CODE - код країни, якій належить IP адреса; GEOIP_COUNTRY_NAME - назва країни, якій належить IP адреса;

The names of the countries and the list of corresponding codes are available here: link to the source

You can get access to data variables that are transferred by the server using .htaccess or the built-in PHP library

To access the values, you can use two types of environment (HTTP and ENV) :

 HTTP:GEOIP_COUNTRY_CODE або ENV:GEOIP_COUNTRY_CODE - .htaccess; $_SERVER['HTTP_GEOIP_ADDR'] або $_SERVER['HTTP_GEOIP_COUNTRY_CODE'] - отримання даних з масиву області змінної $_SERVER для PHP;



block access to all except IP addresses from Ukraine using .htaccess:

 RewriteEngine On RewriteCond %{HTTP:GEOIP_COUNTRY_CODE} !^(UA)$ RewriteRule .* - [F] или SetEnvIf GEOIP_COUNTRY_CODE UA allowed_country Allow from env=allowed_country Deny from all

block access to all except IP addresses from Ukraine and Germany using .htaccess :

 RewriteEngine On RewriteCond %{HTTP:GEOIP_COUNTRY_CODE} !^(UA|DE)$ RewriteRule .* - [F] или SetEnvIf GEOIP_COUNTRY_CODE UA allowed_country SetEnvIf GEOIP_COUNTRY_CODE DE allowed_country Allow from env=allowed_country Deny from all

block access to all users in Germany using .htaccess :

 RewriteEngine On RewriteCond %{HTTP:GEOIP_COUNTRY_CODE} ^(DE)$ RewriteRule .* - [F] или SetEnvIf GEOIP_COUNTRY_CODE DE denied_country Deny from env=denied_country

redirection from the main domain to a multilingual subdomain for all users not from Ukraine, depending on the region ( for example, if the visitor is from Germany - redirect to the DE.DOMAIN_NAME.ZONE subdomain, if from Ukraine - do not redirect ) using .htaccess:

 RewriteEngine On RewriteCond %{HTTP:GEOIP_COUNTRY_CODE} !^(UA)$ RewriteRule ^(.*) https://%{HTTP:GEOIP_COUNTRY_CODE}\.%{HTTP_HOST}/$1 [L,R=301]

— an example of a PHP implementation using the scope of the $_SERVER global variable :

 var_dump($_SERVER['HTTP_GEOIP_ADDR']); var_dump($_SERVER['HTTP_GEOIP_CONTINENT_CODE']); var_dump($_SERVER['HTTP_GEOIP_COUNTRY_CODE']); var_dump($_SERVER['HTTP_GEOIP_COUNTRY_NAME']);




— an example of a PHP implementation using the getenv() function:

 var_dump(getenv(HTTP_GEOIP_ADDR)); var_dump(getenv(HTTP_GEOIP_CONTINENT_CODE)); var_dump(getenv(HTTP_GEOIP_COUNTRY_CODE)); var_dump(getenv(HTTP_GEOIP_COUNTRY_NAME));



— an example of a PHP implementation using
the PHP GeoIP library :

 $client_ip = $_SERVER['REMOTE_ADDR']; var_dump(geoip_country_code_by_name($client_ip)); var_dump(geoip_continent_code_by_name($client_ip)); var_dump(geoip_country_name_by_name($client_ip));