Skip to content

Instantly share code, notes, and snippets.

@turgayozgur
Last active November 20, 2016 00:00
Show Gist options
  • Save turgayozgur/6ad721b2b1c3df7ada07 to your computer and use it in GitHub Desktop.
Save turgayozgur/6ad721b2b1c3df7ada07 to your computer and use it in GitHub Desktop.
Magento2 index.php with GeoIP2
<?php
/**
* Application entry point
*
* Example - run a particular store or website:
* --------------------------------------------
* require __DIR__ . '/app/bootstrap.php';
* $params = $_SERVER;
* $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'website2';
* $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
* $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
* \/** @var \Magento\Framework\App\Http $app *\/
* $app = $bootstrap->createApplication('Magento\Framework\App\Http');
* $bootstrap->run($app);
* --------------------------------------------
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
try {
require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
<div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
<h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
Autoload error</h3>
</div>
<p>{$e->getMessage()}</p>
</div>
HTML;
exit(1);
}
/*
* GeoIP2 block
*/
$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'base';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
try{
require_once __DIR__ . '/lib/internal/GeoIP2/vendor/autoload.php';
} catch (\Exception $ex){
// do not anything.
}
use GeoIp2\Database\Reader;
try{
$reader = new Reader(__DIR__ . '/lib/internal/GeoIP2/db/GeoLite2-Country.mmdb');
$record = $reader->country($_SERVER['REMOTE_ADDR']);
$currentCounty = strtolower($record->country->isoCode);
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = $currentCounty;
}catch (\Exception $e){
// do not anything.
}
/*
* END GeoIP2 block
*/
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment