• home
  • forum
  • my
  • kt
  • download
  • Easy Country from IP with PHP

    Author: 2008-08-16 07:36:23 From:

    Since I’m currently working with advertising related stuff it might be a good thing to learn the whole geographic location by ip number thing. A good starting point is The IP-to-Country Handbook.

    After downloading the CSV from that site it’s time to import:

    First we need a MySQL table for all this:

    CREATE TABLE `blog`.`country_ip` (
      `ip_from` DOUBLE NOT NULL ,
      `ip_to` DOUBLE NOT NULL ,
      `cc2` VARCHAR( 2 ) NOT NULL ,
      `cc3` VARCHAR( 3 ) NOT NULL ,
      `c_name` VARCHAR( 50 ) NOT NULL
    ) ENGINE = MYISAM

    And then import:

    class ip_db_config{
      
    	static $db_name     = "mydb";
    	static $db_password = "";
    	static $db_user	 	  = "root";
    	
    	static function db_connect(){
    		$db_link     = mysql_connect("localhost", self::$db_user, self::$db_password);
    		mysql_select_db(self::$db_name, $db_link);
    	}
    }
    
    function import_country_ips(){
      $file_arr = file("ip-to-country.csv");
      foreach($file_arr as $line){
        $clean_line   = trim( str_replace('"', "'", str_replace("'", "\'", $line) ) );
        $sql          = "INSERT INTO country_ip (ip_from,ip_to,cc2,cc3,c_name) VALUES($clean_line)";
        echo $sql."<br>";
        mysql_query($sql);
      }
      echo "Finished importing";
    }
    
    ip_db_config::db_connect();
    import_country_ips();

    Note the double str_replace(), the first one is needed because of countries like Cote D’ivoire. The second one because a typical row in that CSV file looks like this:

    "33996344","33996351","GB","GBR","UNITED KINGDOM"

    It’s time to test:

    function assoc_query_1D($sql){
      $result = mysql_query($sql);
      if(!$result) 
        return 0;
      else
        return mysql_fetch_assoc($result);
    }
    	
    function check_country($country, $dot_ip){
      
      $ip = ip2long($dot_ip);
      $sql = "SELECT * FROM `country_ip` WHERE ip_from <= $ip and ip_to >= $ip";
      $country_data = assoc_query_1D($sql);
      $strlen = strlen($country);
      
      if($strlen == 2)
        $test_with = $country_data['cc2'];
      else if($strlen == 3)
        $test_with = $country_data['cc3'];
      else
        $test_with = $country_data['c_name'];
        
      if(strtolower($country) == strtolower($test_with))
        return true;
      else
        return false;
    }
    
    ip_db_config::db_connect();
    if(check_country('THA', $_SERVER['REMOTE_ADDR']))
      echo "Sawadee Kup!";

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (13)
      XML and PHP (16)

    New

    Hot