Web Hosting Info

Search:

featured partner

The IP to Country Database

  Forum Topics : Development / php script for non sql users...
Submitted by cassis on Mon, 12/08/2003 - 09:02.
hi guys... i just downloaded your db which is quite well done ;) good job, i didn't have the energy to make something like this nice one !

but my problem is the mysql db size and i had to find another way... my db can't go up to 2Mb, and your file is far bigger with my half-full database... so i've just written some line to help people :

<?php
function GetCountry($ip) {

    // open ip-to-country file, set correct address...
    if ( $file = fopen("ip-to-country.csv","r") ) {

        // if successfully opened, read each line of the file
        while ( $line = fgetcsv($file,1024,",") ) {

            // if our the ip corresponds, return the country name
            // $line[2] = country code 2 char,
            // $line[3] = country code 3 char, if needed...
            if ( ip2long($ip) > $line[0] && ip2long($ip) < $line[1] ) {
                return $line[4];
            }
        }
    }

    // if this ip has something wrong :?
    return false;
}

$country = GetCountry($REMOTE_ADDR);
echo "Your country is ".$country;
?>

Cassis ;)

Comment viewing options:
Select your preferred way to display the comments and click 'Save settings' to submit your changes.
This script is not a good way
Posted by strummer on Mon, 12/08/2003 - 13:47.
This script is not a good way of solving this issue.

There is already a post which deals with this kind of thing here: Non-Mysql Country Detection

----------
Q. Should I put a witty comment below my post?
A. No.
Vet Admin.
Posted by Sarcatonias on Fri, 12/12/2003 - 06:49.
Please tell me how to access the IP of the G.V. (Sonny) Montgomery VAMC (Veterans Administration Medical Center) of Jackson, MS. Thank you.
Vet Admin.
Posted by Sarcatonias on Fri, 12/12/2003 - 06:49.
Please tell me how to access the IP of the G.V. (Sonny) Montgomery VAMC (Veterans Administration Medical Center) of Jackson, MS. Thank you.
Great job with easy structure
Posted by janhendrik on Fri, 01/20/2006 - 07:42.
Hi, nice code. But - what if i am behind a firewall?? If you are behind a firewall the $REMOTE_ADDR might not work. You can replace the section $country = GetCountry($REMOTE_ADDR); with:
if ($_SERVER["HTTP_CLIENT_IP"]) $country = GetCountry($_SERVER["HTTP_CLIENT_IP"]);
else if($_SERVER["HTTP_X_FORWARDED_FOR"]) $country = GetCountry($_SERVER["HTTP_X_FORWARDED_FOR"]);
else if($_SERVER["REMOTE_ADDR"]) $country = GetCountry($_SERVER["REMOTE_ADDR"]);
BG Jan
 
Getting Real IP
Posted by TheJohnDoe2005 on Sun, 01/22/2006 - 01:25.
Please also note that when using the "ip2long" function, certain IPs address gives a negative value. If that happens, add 0xffffffff to the negative value to get the IP Number for comparision in the database.
Read More Here http://www.codepost.org/browse/snippets/34

This is the function I use for detecting real IP Address.
function GetRealIP()
{
    $check = array('HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED','HTTP_VIA', 'HTTP_X_COMING_FROM', 'HTTP_COMING_FROM', 'HTTP_CLIENT_IP', 'REMOTE_ADDR');

    for($i=0; $i<count($check); $i++)
    {
	$item = $check[$i];
	if($found = $_SERVER[$item]) return $found;
    }
}

For my test site http://ip2c.mooo.com/,
I didn't use mysql database to store the 16 bit index database...because it is going to be very slow in searching if i am going to do that. I created a script to split the files accordingly (which took less than 2 minutes). After doing so, each search takes about 0.0005 secs compared to the normal database which (csv file) took about 0.6 secs. (1200x faster) Another thing that speed up the process is the use of AJAX. The page doesn't have to reload each time the user key in to search for a new IP address.

It also auto plots the country's location on a world map that you are in and searching for too.

Regards TheJohnDoe2005
http://thejunglebugsprojects.atspace.com/
Bad IF
Posted by McAfee on Fri, 01/20/2006 - 18:48.
The comparison should use:
>=
<=

Instead of:
>
<

Example:
if ( ip2long($ip) >= $line[0] && ip2long($ip) <= $line[1] ) {