根据 IP 判断是否是欧盟国家

master
LiaoYijun 6 years ago
parent 940c00e4e0
commit fa38497d97

@ -24,6 +24,25 @@ class Index extends CI_Controller
echo json_encode($data);
}
/**
* 根据 IP 判断是否是欧盟国家
* http://localhost:8003/index.php/apps/ip2location/index/is_eu_country?ip_address=159.8.126.74
*/
public function is_eu_country() {
$european_counties = array('Russia', 'Ukraine', 'France');
$ip_address = $this->input->get_post('ip_address');
if (empty($ip_address)) {
$ip_address = $this->input->ip_address();
}
$ip_number = $this->Dot2LongIP($ip_address);
$country_name = $this->Ip2location_db1_model->get_country_name($ip_number);
$is_eu_country = 'false';
if (in_array($country_name, $european_counties)) {
$is_eu_country = 'true';
}
echo json_encode($is_eu_country);
}
function Dot2LongIP($IPaddr)
{
if ($IPaddr == "") {

@ -30,4 +30,21 @@ class Ip2location_db1_model extends CI_Model
}
public function get_country_name($ip_number)
{
$sql = '
SELECT TOP 1 ipdb.country_name
FROM ip2location_db1 ipdb
WHERE ? BETWEEN ipdb.ip_from AND ipdb.ip_to
';
$query = $this->INFO->query($sql, array($ip_number));
if ($query->num_rows() > 0) {
$row = $query->row();
return $row->country_name;
} else {
return FALSE;
}
}
}
Loading…
Cancel
Save