Jump to content

Recommended Posts

(Since some people asked):

IP is just like an internet address, so it depends on the country, the start value will be different for each country, I remember seen this somewhere, and the IP number is a geolical global position... below is a php script that does this:

<?php 
$ip = $_SERVER['REMOTE_ADDR'];//gets user's ip address
$explode = explode(".", $ip);
$ipno = ($explode[0] * 16777216) + ($explode[1] * 65536) + ($explode[2] * 256) + $explode[3];
//up to here, the script converts the IP address to an ip number
$txtfile = file("ipcountries.txt");//get the information from the long ip to country text file
foreach ($txtfile as $line) 
{$explodetxt = explode(",", $line);
$fip = $explodetxt[0];
$sip = $explodetxt[1];
$country = $explodetxt[2];
$country = str_replace("\r\n","",$country);
$country = str_replace(" ","",$country);
if ($ipno > $fip && $ipno < $sip) {$located = $country;}}//sorts out your ip range, and thus find out the range of your country's ip position, giving the result as country.

$countries = file("countries.txt");//get the country text file, and convert it to country name.
foreach ($countries as $ccountry) {$parts = explode(",", $ccountry);
$part1 = str_replace("\r\n","",$parts[1]);
if ($located == $part1) {
echo "You are living in ".$parts[0]."! And your ip adress is $ip!";}}
?>

This is basically the php script, because the actual hard work is the ipcountry.txt file, which is a database storing up to ten thousands of information, therefore, instead of the country name itself, I converted to country symbol which not just make it easier to look, organized, as well as saving the time and space when uploading.

However, what i believe is that the ip will change, and my guess is that the number will always increase, so these database will need to be updated once some time.

The countries txt file which includes the country name and short abbreviation is attached, but the ipcountries.txt is to large to attach, so download it at:

http://tedchou12.110mb.com/ipcountries.txt

feel free to use this on your website.

Ted

Ted

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/36628-ip-to-country-test/#findComment-176121
Share on other sites

If you are looking for a really efficient way to do IP->Country lookups then check out the source code in the Firestats PHP visitor tracker. They have implemented a binary search in PHP and it is very fast and has less overhead than text searching.

Link to comment
https://forums.phpfreaks.com/topic/36628-ip-to-country-test/#findComment-176140
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.