Accessing through PHP
MODX Cloud provides basic GeoIP capabilities. You can leverage this data in your site by reading the $_SERVER array in a PHP script.
The following server variables are natively available for MODX Cloud instances:
- GEO_LONGITUDE: The longitude coordinates determined from the requesting IP address.
- GEO_LATITUDE: The latitude coordinates determined from the requesting IP address.
- GEO_COUNTRY_CODE: The two-letter ISO country code determined from the requesting IP address.
Example:
<?php
$redirected = $_SESSION['geo_redirect'] ?? false;
// Send to a country picker if country code is not "US"
if (!$redirected && $_SERVER['GEO_COUNTRY_CODE'] !== 'US') {
$_SESSION['geo_redirect'] = true;
header('Location: http://www.example.com/select-country/');
}Accessing through nginx
MODX Cloud also provides basic GeoIP data to your nginx configuration for web rules.
The following nginx variables are natively available for MODX Cloud instances:
- $geoip2_data_longitude: The longitude coordinates determined from the requesting IP address.
- $geoip2_data_latitude: The latitude coordinates determined from the requesting IP address.
- $geoip2_data_country_code: The two-letter ISO country code determined from the requesting IP address.
Example:
if ($geoip2_data_country_code != "US") {
return 301 https://international.example.com;
}