例子:
<?php
$geoip = new \lysenkobv\GeoIP\GeoIP();
$ip = $geoip->ip(Yii::$app->request->getUserIP());
$currencies = json_decode(file_get_contents('data.json'), true);
foreach ($currencies as $currency) {
if (strtoupper($currency['country']) == strtoupper($ip->isoCode)) {
// set session and other jobs
}
}
例如
config/main.php
(用于
yii2-app-advanced
)以及
config/web.php
(用于
yii2-app-basic
) :
<?php
return [
//
'on beforeAction' => function ($action) {
if (!Yii::$app->session->has('global-currency')) {
// your code for get found user $currency, maybe from db or ip
Yii::$app->session->set('global-currency', $currency);
}
}
];