한국 ip list 범위 체크 및 데이터 갱신

  • 마루디자인
  • 2015.03.30 09:41:24
  • 0

getKoreaNationIpList(); 로 json 파일 저장 한번 하고,

echo matchKoreaNationIp('203.238.183.231') ? '한국' : '외국';

License : BSD

1
2
3
4
5
6
7
8
9
10
11
cidr 로 하면 데이터가 너무 커서 버림.
 
데이터가 json 이니 js 버전도 가능.
 
 
 
getKoreaNationIpList(); 로 json 파일 저장 한번 하고,
 
echo matchKoreaNationIp('203.238.183.231') ? '한국' : '외국';
 
License : BSD

[ ▼ 참고 Source ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function matchKoreaNationIp($ip) {
  static $nip = null;
  if(null === $nip) $nip = json_decode(file_get_contents(dirname(__FILE__).'/ipv4list.ko.json'));
  $ip = sprintf('%08x', ip2long($ip));
  $if = substr($ip, 0, 2);
  if(!isset($nip->{$if})) return false;
  $nf = &$nip->{$if};
  foreach($nf as $n) {
    if($n[0] > $ip) continue;
    if($n[1] < $ip) continue;
    return true;
  }
  return false;
}
 
function getKoreaNationIpList() {
  $xml = simplexml_load_file('http://ip.kisa.or.kr/ipas/jsp/api/ipv4list.jsp');
 
  $ips = array();
  foreach($xml->ipv4 as $ipv4) {
    $ips[$ipv4->sno.''] = $ipv4->eno;
  }
  ksort($ips);
 
  $ranges = array();
 
  $beno = 0;
  $sf = '';
  foreach($ips as $sip => $eip) {
    echo $sip, ' ', $eip, "\n";
    $sno = ip2long($sip);
    if($sno == $beno) {
      $sh = array_pop($ranges[$sf])[0];
    } else {
      $sh = sprintf('%08x', $sno);
    }
    $eno = ip2long($eip);
    $eh = sprintf('%08x', $eno);
    $sf = substr($sh, 0, 2);
    if(!isset($ranges[$sf])) $ranges[$sf] = array();
    $ranges[$sf][] = array($sh, $eh);
    $beno = $eno + 1;
  }
 
  file_put_contents(dirname(__FILE__).'/ipv4list.ko.json', json_encode($ranges));
}

TagList

  • Doesn't exist

AttachmentFile List

  • Doesn't exist
목록으로 돌아가기
Write a comment