ip2country using csv help please

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    ip2country using csv help please

    i have a csv file the contents of this like
    Code:
    startip,endip,countrycode,countryname
    16777216,16777471,AU,Australia
    16777472,16777727,CN,China
    16777728,16778239,CN,China
    16778240,16779263,AU,Australia
    16779264,16781311,CN,China
    16781312,16785407,JP,Japan
    16785408,16793599,CN,China
    16793600,16809983,JP,Japan
    16809984,16842751,TH,Thailand
    16842752,16843007,CN,China
    16843008,16843263,AU,Australia
    16843264,16843775,CN,China
    16843776,16844799,CN,China
    .......................................
    Database description

    database in CSV format. It has 4 fields namely 'startip', 'endip', 'countrycode', and 'countryname'. Below is the description of each of the fields:

    'startip'
    Represents the starting IPv4 address in 32-bit decimal form.

    'endip'
    Represents the ending IPv4 address in 32-bit decimal form.

    'countrycode'
    ISO 3166-1 alpha-2 country code

    'countryname'
    Country name

    Converting dot-decimal IP address notation to 32-bit decimal form

    Let's assume that your IP address is : 12.34.56.78

    The 32-bit decimal form value:
    = (16777216 x 12) + (65536 x 34) + (256 x 56) + (7
    = 203569230

    OR

    The 32-bit decimal form value:
    = ( 12 <<24) + ( 34 <<16) + ( 56 << + 78
    = 203569230

    Therefore the 32-bit decimal value = 203569230

    Below is a sample function in PHP that will do the convertion
    PHP Code:
    <?php

    function convertIPtoInteger($ip)
    {
        
    $ipArr explode('.',$ip);
        return (
    $ipArr[0]<<24) + ($ipArr[1]<<16) + ($ipArr[2]<<8) + $ipArr[3];
    }

    ?>


    can any one help me how to get the country code for every ip using this file?

    #2
    add the csv database to your mysql database
    and use this query:
    PHP Code:
    SELECT countrycode,countryname FROM your_ip2location_database_name WHERE '".convertIPtoInteger($ip)."' >= startip AND '".convertIPtoInteger($ip)."' <= endip 
    Advertise your mobile site for FREE with AdTwirl

    Comment


      #3
      thanks gum bro, but without using mysql is it not possible to use that file using fopen() or anything like to use that... i dont want to use the sql thats why thinking about this...

      Comment


        #4
        Yes, its possible & very easy try this

        PHP Code:
        <?php
        function readCSV($filename) {
            
        $rows = array();
            foreach (
        file($filenameFILE_IGNORE_NEW_LINES) as $lines) {
                
        $rows[] = str_getcsv($lines);
            }
            return 
        $rows;
        }
        print_r(readCSV("file.csv"));
        I need some facebook likes, can you please help me
        http://facebook.com/softwarefreakin
        I noticed social media is really powerful
        Well DONE is better than well SAID

        Comment


          #5
          Originally posted by softwarefreak View Post
          Yes, its possible & very easy try this

          PHP Code:
          <?php
          function readCSV($filename) {
              
          $rows = array();
              foreach (
          file($filenameFILE_IGNORE_NEW_LINES) as $lines) {
                  
          $rows[] = str_getcsv($lines);
              }
              return 
          $rows;
          }
          print_r(readCSV("file.csv"));
          but how to get country against any ip using this freak bro?

          Comment


            #6
            explode each line by comma(,) and run a sql query to add data to a mysql db.

            use 'BETWEEN' to get country name of an IP.

            Comment


              #7
              PHP Code:
              <?php
              $databasehost 
              "localhost";
              $databasename "";
              $databaseusername ="";
              $databasepassword "";
              $con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
              @
              mysql_select_db($databasename) or die(mysql_error());


              if(isset(
              $_POST['submit']))
                 {
                   
              $filename=$_POST['filename'];
                   
              $handle fopen("$filename""r");
                   while ((
              $data fgetcsv($handle100000",")) !== FALSE)
                   {
                  
                     
              $import="INSERT into iptocountry(startip,endip,countrycode,countryname) values('$data[0]','$data[1]','$data[2]','$data[3]')";
                     
              mysql_query($import) or die(mysql_error());
                   }
                   
              fclose($handle);
                   print 
              "Import done";
                 }
                 else
                 {
               
                    print 
              "<form action='import.php' method='post'>";
                    print 
              "Type file name to import:";
                    print 
              "<input type='text' name='filename' size='20'>";
                    print 
              "<input type='submit' name='submit' value='submit'></form>";
                 }
                 
              ?>
              this will simply dump the csv into database....and have done it...

              Comment


                #8
                Razor you are cool...dude.thank for this

                Comment

                Working...
                X