You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.0 KiB
PHTML

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Index extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Ip2location_db1_model');
}
public function index()
{
$data = array();
//优先读取参数ip地址没有则自动判断客户端地址
$data['ip_address'] = $this->input->get_post('ip_address');
if (empty($data['ip_address'])) {
$data['ip_address'] = $this->input->ip_address();
}
$data['ip_number'] = $this->Dot2LongIP($data['ip_address']);
$data['country_code'] = $this->Ip2location_db1_model->get_country_code($data['ip_number']);
echo json_encode($data);
}
function Dot2LongIP($IPaddr)
{
if ($IPaddr == "") {
return 0;
} else {
$ips = explode(".", "$IPaddr");
return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
}
}
}