GeoIP alternative to get IP location – Google App Engine PHP Recipes

Today’s recipes is all about to get very basic information about your visitor’s IP address location. Unfortunately, in Google App Engine, PHP GeoIP extension is not enabled in their standard environment. So sometimes, it’s essential for application developer to get the visitor’s IP address location.

Here, you can easily get user’s IP address location with 3 information easily and by default from Google App Engine. In GAE, if you will discover that Google app engine itself added few extra _X_headers in $_SERVER super-global variable. I am dumping the $_SERVER variable here.

<?php
//Output of $_SERVER

/*[
    'APPLICATION_ID' => 'YOUR GAE APPLICATION ID',
    'AUTH_DOMAIN' => 'gmail.com',
    'CURRENT_MODULE_ID' => 'default',
    'CURRENT_VERSION_ID' => '20170319t185047.399949465273621812',
    'DEFAULT_VERSION_HOSTNAME' => 'xxxxxxxxxx.appspot.com',
    'DOCUMENT_ROOT' => '/base/data/home/apps/xxxxxxxxxxxxxx/20170319t185047.399949465273621812',
    'HTTPS' => 'on',
    'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*\/*;q=0.8',
    'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8,bn;q=0.6,ms;q=0.4,pt;q=0.2',
    'HTTP_HOST' => 'xxxxxxxxxx.appspot.com',
    'HTTP_UPGRADE_INSECURE_REQUESTS' => '1',
    'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36',
    'HTTP_X_APPENGINE_CITY' => 'dhaka',
    'HTTP_X_APPENGINE_CITYLATLONG' => '23.709921,90.407143',
    'HTTP_X_APPENGINE_COUNTRY' => 'BD',
    'HTTP_X_APPENGINE_REGION' => '?',
    'HTTP_X_CLOUD_TRACE_CONTEXT' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxxx;o=x',
    'INSTANCE_ID' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'PATH_TRANSLATED' => '/base/data/home/apps/xxxxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxx/index.php',
    'PHP_INI_SCAN_DIR' => '/base/data/home/apps/xxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxx/',
    'PHP_SELF' => '/index.php',
    'REMOTE_ADDR' => 'xxx.xxx.xxx.xxx',
    'REQUEST_ID_HASH' => 'XXXXXXXX',
    'REQUEST_LOG_ID' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'REQUEST_METHOD' => 'GET',
    'REQUEST_URI' => '/',
    'SCRIPT_FILENAME' => '/base/data/home/apps/xxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/index.php',
    'SCRIPT_NAME' => '/index.php',
    'SERVER_NAME' => 'xxxxxxxxxxxxxxxxxxx.appspot.com',
    'SERVER_PROTOCOL' => 'HTTP/1.1',
    'SERVER_SOFTWARE' => 'Google App Engine/1.9.48',
    'USER_IS_ADMIN' => '0',
    'REQUEST_TIME_FLOAT' => 148992.7,
    'REQUEST_TIME' => 14899,
    'argv' =>
        array(),
    'argc' => 0,
];*/


//Get the visitor's IP details
$visitorIp = [
    'ip' => $_SERVER['REMOTE_ADDR'],
    'HTTP_X_APPENGINE_CITY' => $_SERVER['HTTP_X_APPENGINE_CITY'] ?: null,
    'HTTP_X_APPENGINE_CITYLATLONG' => $_SERVER['HTTP_X_APPENGINE_CITYLATLONG'] ?: null,
    'HTTP_X_APPENGINE_COUNTRY' => $_SERVER['HTTP_X_APPENGINE_COUNTRY'] ?: null,
    'HTTP_X_APPENGINE_REGION' => $_SERVER['HTTP_X_APPENGINE_REGION'] ?: null
];

 

And from that, we can easily get the visitor’s City, City-level lat long, country and region that primary will help you to get visitor’s location.

Shaharia is a professional software engineer with more than 10 years of experience in the relevant fields. Digital ad certified, cloud platform architect, Big data enthusiasts, tech early adopters.