增加通过 IP 获取国家信息

master
Jimmy Liow 2 years ago
parent 9e241a0f1b
commit f0c634d361

@ -24,6 +24,17 @@ class Index extends CI_Controller
echo json_encode($data);
}
public function get_country() {
$data = array();
//优先读取参数ip地址没有则自动判断客户端地址
$data['ip_address'] = $this->input->get_post('ip_address');
if (empty($data['ip_address'])) {
$data['ip_address'] = $this->input->ip_address();
}
$country = $this->Ip2location_db1_model->get_country_by_ip($data['ip_address']);
echo json_encode($data);
}
/**
* 根据 IP 判断是否是欧盟国家
* http://localhost:8003/index.php/apps/ip2location/index/is_eu_country?ip_address=159.8.126.74

@ -13,6 +13,33 @@ class Ip2location_db1_model extends CI_Model
}
public function get_country_by_ip($ip_address)
{
$sql = "
SELECT TOP 1 ipdb.country_code, ipdb.country_name
FROM ip2location_db1 ipdb
WHERE ? BETWEEN ipdb.ip_from AND ipdb.ip_to
";
$query = $this->INFO->query($sql, array($ip_address));
$country = array(
'country_code' => '',
'country_name' => ''
);
if ($query->num_rows() > 0) {
$row = $query->row();
$country = [
'ip_address' => $ip_address,
'country_code' => $row->country_code,
'country_name' => $row->country_name
];
}
return $country;
}
public function get_country_code($ip_number)
{
$sql = "

Loading…
Cancel
Save