// set default currency code if not set already in index()
if (!$this->session->userdata('currency_code')) {
$this->session->set_userdata('currency_code', 'USD');
$this->data['ajax_currency'] = true;
}
...
public function set_currency_ajax() {
$country_info = file_get_contents('https://freegeoip.net/json/' . $this->input->ip_address());
$country_info = json_decode($country_info, true);
$countries_in_euro = array('AT', 'BE', 'CY', 'DE', 'EE', 'FI', 'FR', 'GR', 'IE', 'IT', 'LT', 'LV', 'LU', 'MT', 'NL', 'PT', 'SK', 'SI', 'ES');
if (in_array($country_info['country_code'], $countries_in_euro)) {
$this->session->set_userdata('currency_code', 'EUR');
}
else if ($country_info['country_code'] == 'GB') {
$this->session->set_userdata('currency_code', 'GBP');
}
else {
$this->session->set_userdata('currency_code', 'USD');
}
$response = array(
'sess_currency' => strtolower($this->session->currency_code),
);
// echo json_encode($response);
$this->output->enable_profiler(false)->set_output(json_encode($response))->_display();
exit();
}