IP 주소로 접속 지역 알아내기, 다국어 > php

본문 바로가기
사이트 내 전체검색

php

IP 주소로 접속 지역 알아내기, 다국어

페이지 정보

작성자 서방님 댓글 0건 조회 64회 작성일 19-09-02 14:26

본문

http://www.ip2nation.com 


위의 사이트에 가보시면 Download 메뉴를 클릭하시면 SQL 파일을 받아보실수 있습니다.

이 SQL 파일 덤프를 자신의 디비에 밀어넣으신 후

쿼리만으로 현재 접속한 자의 국가를 알수 있습니다.


위의 사이트에서 샘플 소스도 제공하고 있습니다.



  1. <?php
  2.    
  3.     $server   = ''; // MySQL hostname
  4.     $username = ''; // MySQL username
  5.     $password = ''; // MySQL password
  6.     $dbname   = ''; // MySQL db name
  7.    
  8.    
  9.     $db = mysql_connect($server, $username, $password) or die(mysql_error());
  10.           mysql_select_db($dbname) or die(mysql_error());
  11.    
  12.     $sql = 'SELECT
  13.                 c.country
  14.             FROM
  15.                 ip2nationCountries c,
  16.                 ip2nation i
  17.             WHERE
  18.                 i.ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
  19.                 AND
  20.                 c.code = i.country
  21.             ORDER BY
  22.                 i.ip DESC
  23.             LIMIT 0,1';
  24.    
  25.     list($countryName) = mysql_fetch_row(mysql_query($sql));
  26.    
  27.     // Output full country name
  28.     echo $countryName;
  29.    
  30. ?> 

 


첫번째 샘플 소스를 확인해 보면 위의 쿼리문으로 해당 아이피의 지역을 알수 있습니다.

응용하면 두번째 샘플 소스로 활용이 가능합니다.



  1. <?php
  2.    
  3.     $server   = ''; // MySQL hostname
  4.     $username = ''; // MySQL username
  5.     $password = ''; // MySQL password
  6.     $dbname   = ''; // MySQL db name
  7.    
  8.    
  9.     $db = mysql_connect($server, $username, $password) or die(mysql_error());
  10.           mysql_select_db($dbname) or die(mysql_error());
  11.            
  12.     $sql = 'SELECT
  13.                 country
  14.             FROM
  15.                 ip2nation
  16.             WHERE
  17.                 ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
  18.             ORDER BY
  19.                 ip DESC
  20.             LIMIT 0,1';
  21.    
  22.     list($country) = mysql_fetch_row(mysql_query($sql));
  23.    
  24.     switch ($country) {
  25.         case 'se':
  26.             // Get the Swedish to a Swedish newssite
  27.             header('Location: http://www.thelocal.se/');
  28.             exit;
  29.         case 'us':
  30.             // And redirect US visitors to CNN
  31.             header('Location: http://www.cnn.com/');
  32.             exit;
  33.         default:
  34.             // The rest of the world can go to BBC
  35.             header('Location: http://www.bbc.co.uk/');
  36.             exit;
  37.     }
  38.    
  39. ?>


위와 같이 접속 지역에 따라 다국어 사이트 운영시 해당 국가 사이트로 이동하게 응용도 가능합니다.

댓글목록

등록된 댓글이 없습니다.

Total 612건 4 페이지
게시물 검색

회원로그인

접속자집계

오늘
69
어제
84
최대
1,347
전체
154,440
Latest Crypto Fear & Greed Index

그누보드5
Copyright © 서방님.kr All rights reserved.