ip2country using csv help please

Started on the old forum in September 2012 · view the original in the archive · old links redirect here

7 replies · 1,022 views · started 14 years ago · 1 min read

5 participants
razor Developer · 52 posts #1

i have a csv file the contents of this like

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) + (78)
= 203569230

OR

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

Therefore the 32-bit decimal value = 203569230

Below is a sample function in PHP that will do the convertion

**```php

<?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?
GumSlone Administrator Principal · 1,690 posts #2

add the csv database to your mysql database and use this query:

SELECT countrycode,countryname FROM your_ip2location_database_name WHERE '".convertIPtoInteger($ip)."' >= startip AND '".convertIPtoInteger($ip)."' <= endip

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

razor Developer · 52 posts #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...

softwarefreak Senior · 349 posts #4

Yes, its possible & very easy try this

<?php
function readCSV($filename) {
	$rows = array();
	foreach (file($filename, FILE_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

razor Developer · 52 posts #5

softwarefreak wrote: Yes, its possible & very easy try this

<?php
function readCSV($filename) {
	$rows = array();
	foreach (file($filename, FILE_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?

razor Developer · 52 posts #7
<?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($handle, 100000, ",")) !== 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...

slcash Developer · 75 posts #8

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

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close