« Previous entry | Next entry » Browse > Snippets
Skip to comments (13)
Converting Ip Address To Country [Working Example]
Posted by TheJohnDoe2005 on Oct 17 2005 @ 08:29 :: 5410 unique visits
PHP:Converting Ip Address To Country [Working Example]
SAMPLE IMAGE OF THE PROGRAM

Well this is my very first contribution to the CodePost. Hope you all will like this one.
Description
Simple Web Application that uses the Ip Address to search for the country through using the FREE database created by IP-To-Country.com. There is NO need to import to other Database application. (ie MySQL, MSSQL or Mirosoft Access). Runs directly from file. It took me less than a second to search for the result.
How to install
Step 1
- Download the database file from IP-To-Country.com. Extract the .csv file to your web directory.
Step 2
- Copy the code below to your fave text editor and save it as "index.php". Place it together with your database file from Step 1.
Step 3
- Rename the "$filename" variable in the code to the filename of your database from step 1.
Step 4
- Start your Apache Server and run the application.
Information
Programing Type : Web Application
Programing Lang : Php / Html
Software Needed : Apache Server 2.0.55 / PHP 5.05
Database Type : csv (Comma Seperated Valued File)
About
Coded by : TheJohnDoe2005
Version : 1.1
Date Created : 17th October 2005
Date Updated : 25th November 2005
Email Address: Email Me
Website : http://thejunglebugsprojects.atspace.com/CIPATC/
Disclaimer
The database provided by IP-To-Country.com may not be accurate.
Thanks and Credits
Please include the lines below in your application if you plan to use their database online...
"This 'work' uses the IP-to-Country Database provided by WebHosting.Info(http://www.webhosting.info),available from http://ip-to-country.webhosting.info."
More Information
This application did not verify the Ip Address. You could add in some verification for it. You could also do some indexing for the database for faster searching too.
CODE: PHP
<?php
if($_REQUEST['submit'] || $_REQUEST['IpAddress'])
{
// Enter the filename of the database file downloaded from ip-to-country.com
$filename = "Country.csv";
$IpAddress = $_REQUEST['IpAddress'];
$found = FALSE;
// Open File in Read-Only Mode
if($filehandle = fopen($filename, "r"))
{
while($data = fgetcsv($filehandle, 1000, ","))
{
if($IpAddress != NULL)
{
$IpNumber = ComputeIPNumber($IpAddress);
if($IpNumber >= $data[0] && $IpNumber <= $data[1])
{
echo "The Country for Ip Address " . $IpAddress . " is " . $data[4];
$found = TRUE;
break;
}
}
else
{
echo "Error at Level 2. Ip Address is Empty or Null !";
exit;
}
}
}
else
echo "Error at Level 1. Cannot Open Database File. Please Try Again !";
if(!$found)
echo "Error at level 0. Country Not Found For Ip Address";
fclose($filehandle);
}
function ComputeIPNumber($IpAddress)
{
// Method 1
// $valuex = (($value[0] << 24) | ($value[1] << 16) | ($value[2] << 8) | $value[3]) ;
// if($valuex < 0)
// $newTotalSum = pow(2, 32) + $valuex;
// else
// $newTotalSum = $valuex;
// return $newTotalSum;
// Method 2
$valuex = ip2long($IpAddress);
if($valuex < 0)
$newTotalSum = 0xffffffff + $valuex;
else
$newTotalSum = $valuex;
return $newTotalSum;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Converting Ip Address to Country Version 1.1</title>
<meta http-equiv="content-language" content="en-us" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="Description" content="TheJohnDoe Production © | Better To Be Unknown Then Known" />
<meta name="Author" content="The JohnDoe2005 . ©Copyright 1998 - 2005" />
<style type="text/css">img {border:0px}</style>
</head>
<body>
<form method="post" action="index.php">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<th align="left" colspan="2">Converting Ip Address to Country</th>
</tr>
<tr>
<td>Enter Ip Address : </td>
<td><input type="text" name="IpAddress" maxlength="15" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
<tr>
<td colspan="2"><br/></td>
</tr>
</table>
</form>
<p>Created By TheJohnDoe2005. Dated 17th October 2005</p>
<p><a href="http://validator.w3.org/check?uri=referer">
<img src="http://www.w3.org/Icons/valid-xhtml10"alt="Valid XHTML 1.0 Strict" height="31" width="88" />
</a></p>
</body>
</html>
if($_REQUEST['submit'] || $_REQUEST['IpAddress'])
{
// Enter the filename of the database file downloaded from ip-to-country.com
$filename = "Country.csv";
$IpAddress = $_REQUEST['IpAddress'];
$found = FALSE;
// Open File in Read-Only Mode
if($filehandle = fopen($filename, "r"))
{
while($data = fgetcsv($filehandle, 1000, ","))
{
if($IpAddress != NULL)
{
$IpNumber = ComputeIPNumber($IpAddress);
if($IpNumber >= $data[0] && $IpNumber <= $data[1])
{
echo "The Country for Ip Address " . $IpAddress . " is " . $data[4];
$found = TRUE;
break;
}
}
else
{
echo "Error at Level 2. Ip Address is Empty or Null !";
exit;
}
}
}
else
echo "Error at Level 1. Cannot Open Database File. Please Try Again !";
if(!$found)
echo "Error at level 0. Country Not Found For Ip Address";
fclose($filehandle);
}
function ComputeIPNumber($IpAddress)
{
// Method 1
// $valuex = (($value[0] << 24) | ($value[1] << 16) | ($value[2] << 8) | $value[3]) ;
// if($valuex < 0)
// $newTotalSum = pow(2, 32) + $valuex;
// else
// $newTotalSum = $valuex;
// return $newTotalSum;
// Method 2
$valuex = ip2long($IpAddress);
if($valuex < 0)
$newTotalSum = 0xffffffff + $valuex;
else
$newTotalSum = $valuex;
return $newTotalSum;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Converting Ip Address to Country Version 1.1</title>
<meta http-equiv="content-language" content="en-us" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="Description" content="TheJohnDoe Production © | Better To Be Unknown Then Known" />
<meta name="Author" content="The JohnDoe2005 . ©Copyright 1998 - 2005" />
<style type="text/css">img {border:0px}</style>
</head>
<body>
<form method="post" action="index.php">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<th align="left" colspan="2">Converting Ip Address to Country</th>
</tr>
<tr>
<td>Enter Ip Address : </td>
<td><input type="text" name="IpAddress" maxlength="15" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
<tr>
<td colspan="2"><br/></td>
</tr>
</table>
</form>
<p>Created By TheJohnDoe2005. Dated 17th October 2005</p>
<p><a href="http://validator.w3.org/check?uri=referer">
<img src="http://www.w3.org/Icons/valid-xhtml10"alt="Valid XHTML 1.0 Strict" height="31" width="88" />
</a></p>
</body>
</html>
13 comments posted so far
Add your own »
2. On Oct 17 2005 @ 09:27 Niek wrote:
In addition to the comment above, after looking at a comment in the linked snipped I posted earlier, I think ip2long() can do the same.3. On Oct 17 2005 @ 11:57 TheJohnDoe2005 wrote:
Dear Niek,By using your return statement, there is a need to do some additional check.
Partly because the database are all signed values....some IP Address will returns with unsigned values (or negative values) if your method is apply.
For example,
Ip Address : 222.1.1.1
Ip Number : -570359551
This can be solved if you check if the return value is a negative value,
CODE: PHP
$valuex = (($value[0] << 24) | ($value[1] << 16) | ($value[2] << 8) | $value[3]) ;
if($valuex < 0)
$newTotalSum = pow(2, 32) + $valuex;
else
$newTotalSum = $valuex;
return $newTotalSum;
if($valuex < 0)
$newTotalSum = pow(2, 32) + $valuex;
else
$newTotalSum = $valuex;
return $newTotalSum;
The Shorter Version if you are using ip2long,
CODE: PHP
I have also updated the above code for users to see the 3 method. =) Cheers
4. On Oct 17 2005 @ 12:26 Niek wrote:
TheJohnDoe2005: you are completely right, I forgot about that.Quick tip: calculating pow(2, 32) is a bit of overhead, adding 0xffffffff to the value is faster.
5. On Oct 18 2005 @ 03:18 Snyke wrote:
The thing that takes longest still is the parsing of the CSV-File anyway.6. On Nov 29 2005 @ 16:54 mondalaci wrote:
Hey JohnDoe,I've written a Python script that spilts the ip-to-country CSV file into 100 pieces and saves them as new CSV segment files and generates their related index and API code in PHP. My solution is obviously 100 times faster this way.
I think it's a very useful tool and I'd like to release it under the General Public License like all my projects. I've used some lines of code from here (which I improved upon), so I'd like to ask you whether I'm allowed to release this stuff under the GPL.
Of course I'd give credit all of you. Both to CodePost and you.
Thanks in advance.
--
Laci
Blog: http://monda.hu/~laci/blog
Home: http://mondalaci.objectis.net
7. On Nov 30 2005 @ 02:36 TheJohnDoe2005 wrote:
mondalaci,Sure by all means...go ahead...
=)
More Information
I have seen another GPL code done in Pure C that searches 3.2 Million lookup per second.
Although it can be integrated with php, the search was reduce to 59000 lookup per second.
I didn't do any indexing for this source because i wanted the user to be able to use it straight away after they have downloaded the database.
System Use
Tested on P4 2.0 Ghz 512 Ram
Pure PHP : 1000++ lookup/sec
Pure C : 3.2 Million lookup/sec
Module PHP : 59000 lookup/sec
Python : 11000 lookup/sec
C# : 1.17 Million lookup/sec
All of the tests uses binary database which was converted from the original *.csv database.
http://weirdsilence.net/software/ip2c
8. On Dec 02 2005 @ 23:21 mondalaci wrote:
John,The ip2c project you referenced seems great. However my work wasn't futile, because it has (minor) additional features and doesn't require administrative privileges.
Please check out this project of mine under the name "IP to Country Splitter" and let me know what you think.
9. On Dec 04 2005 @ 14:55 TheJohnDoe2005 wrote:
mondalaci,I have seen your php coding, here is something you could change. BTW, I have also drop you an email. Check it out =)
CODE: PHP
// Original Function from mondalaci
function value_to_ip($value)
{
if ($value == "")
return 0;
else
{
$w = (int)(($value/16777216) - 256*floor(($value/16777216)/256));
$x = (int)(($value/65536) - 256*floor(($value/65536)/256));
$y = (int)(($value/256) - 256*floor(($value/256)/256));
$z = (int)(($value) - 256*floor(($value)/256));
return ($w.".".$x.".".$y.".".$z);
}
}
function value_to_ip($value)
{
if ($value == "")
return 0;
else
{
$w = (int)(($value/16777216) - 256*floor(($value/16777216)/256));
$x = (int)(($value/65536) - 256*floor(($value/65536)/256));
$y = (int)(($value/256) - 256*floor(($value/256)/256));
$z = (int)(($value) - 256*floor(($value)/256));
return ($w.".".$x.".".$y.".".$z);
}
}
CODE: PHP
// You could use this function and replace all the coding instead. =)
function value_to_ip($value)
{
if ($value == "")
return FALSE;
else
$IpAddress = long2ip($value); // Click on the long2ip for more details
return $IpAddress;
}
function value_to_ip($value)
{
if ($value == "")
return FALSE;
else
$IpAddress = long2ip($value); // Click on the long2ip for more details
return $IpAddress;
}
10. On Apr 29 2009 @ 11:42 guest wrote:
When the wolf wow gold finally found the hole Buy Wow Gold in the chimney he Cheap WoW Gold crawled down and KERSPLASH right into that kettle of water cheapest wow gold and that was the end of his troubles with the big bad wolf.game4power,buy cheap wow gold
WOW GOLD
The next day the wow gold cheap little pig invited his mother over . She said "You see it is just as buy gold wowI told you. The way to Wow Goldget along in the world is to do things as well as you can." Fortunately for that world of warcraft gold little pig, he Cheapest wow Goldlearned that lesson. And he just lived happily ever after!
11. On Jun 25 2009 @ 04:14 guest wrote:
DVD Audio RipperFree DVD Audio Ripper
DVD Audio Ripper for Mac
DVD to MP4 Converter
DVD to MP4 Converter for Mac
DVD to iPod Converter
free DVD to iPod Converter
DVD to iPod Converter for Mac
DVD Ripper
Diablo 2 CD Key
Diablo 2 CD Keys
Free DVD Ripper
DVD Ripper for Mac
NFL Jerseys
Scoccer Jerseys
Hockey Jerseys
MLB Jerseys
NHL Jerseys
Nike Sneakers
12. On Jul 14 2009 @ 04:04 guest wrote:
buy wow goldmy wow power leveling
buy wow gold
good wow power leveling
BUY wow gold
my wow power leveling
CHEAP rs gold
cheap wow power leveling
CHEAPEST lotro gold
MY aion gold
buy wow gold
cheap wow gold
CHEAPEST wow gold
1. On Oct 17 2005 @ 09:21 Niek wrote:
Very nice snippet :)The ComputeIPNumber function can be speed up a bit, by using byte shifting:
{
$value = explode(".", $IpAddress);
return ( ($value[0] << 24) | ($value[1] << 16) | ($value[2] << 8) | $value[3] );
}