Board logo

标题: 转:Country identification based on IP address [打印本页]

作者: 网普科技     时间: 2005-3-3 08:07 PM    标题: 转:Country identification based on IP address

1:Introductionm: country identification without databases
2:Basic script for country identification of IP
3:Getting the IP address of visitors and displaying the country
4:Getting the IP address of visitors and displaying three letters code or complete country name
5:Getting the IP address of visitors,  and displaying country and flag
作者: 网普科技     时间: 2005-3-3 08:09 PM    标题: 1:Introduction: country identification without databases

This script was originally developed for geolocalization of visitors to my site. I had not database access, so in order to make the script fast, I splitted the information from IP-to-Country database (provided by Directi) into several smaller files. All those files may be download. Check this sample file.   

Bellow you will  find examples of code allowing to display the country.
作者: 网普科技     时间: 2005-3-3 08:10 PM    标题: 2:Basic script for country identification of IP

In order to use this script, download compressed Database files in the top of this page, save them within a directory named "ip_files", and use the function bellow to get a two letters country code.
CODE:  [Copy to clipboard]
<?php
$two_letter_country_code=iptocountry("101.102.103.104");

function iptocountry($ip) {   
    $numbers = preg_split( "/\./", $ip);   
    include("ip_files/".$numbers[0].".php");
    $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);   
    foreach($ranges as $key => $value){
        if($key<=$code){
            if($ranges[$key][0]>=$code){$country=$ranges[$key][1];break;}
            }
    }
    if ($country==""){$country="unkown";}
    return $country;
}
?>
How the function works:

Line 5: IP address will be split into individual numbers and saved to an array($numbers).
Line 6: Based in the first number of the IP address ("101" in the example), a PHP file in ip_files/ directory will be included (in the example the file to be included will be "ip_files/101.php"). This file has known country codes for IP addresses starting with the selected first number (p.e: 101.###.###.###, where # is any digit).
Line 7: IP address is transform into appropriate code.
Line 8-12: Data from "ip_files/101.php" is checked in order to find a range of codes which includes the code obtained by transforming our IP.
Line 13: In case IP address is not included in the database, the value for $country will be "unkown".

Line 14: Two letter country code is returned. In case no matches are obtained, the function will return "" (nothing).

Hope it works fine for you.
作者: 网普科技     时间: 2005-3-3 08:11 PM    标题: 3:Getting the IP address of visitors and displaying the country

Getting the IP address of visitors and displaying the country

In case we want to identify geographical location of visitors, we must get from them the IP address.


The IP address of visitors will be contained in the enviromental variable $REMOTE_ADDR


In the example bellow, depending upon country code, info 1 or info 2 is shown:
CODE:  [Copy to clipboard]
<?
$IPaddress=$REMOTE_ADDR;
$two_letter_country_code=iptocountry($IPaddress);

if ($two_letter_country_code=="US"){
    print "This is ad number 1, because you are from USA";
    }else{
    print "This is ad number 2, because you are not from USA";
    }

function iptocountry($ip) {   
    $numbers = preg_split( "/\./", $ip);   
    include("ip_files/".$numbers[0].".php");
    $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);   
    foreach($ranges as $key => $value){
        if($key<=$code){
            if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;}
            }
    }
    if ($two_letter_country_code==""){$two_letter_country_code="unkown";}
    return $two_letter_country_code;
}
?>

作者: 网普科技     时间: 2005-3-3 08:13 PM    标题: 4:Getting the IP address of visitors and ^^^

Getting the IP address of visitors and displaying three letters country code or complete country name

On request, we have added to the compressed document a file named countries.php.
This file may be used to display three letters country code or complete name of country. The code bellow allows getting both data:
CODE:  [Copy to clipboard]
<?
$IPaddress=$REMOTE_ADDR;
$two_letter_country_code=iptocountry($IPaddress);
  
include("IP_FILES/countries.php");
$three_letter_country_code=$countries[ $two_letter_country_code][0];
$country_name=$countries[$two_letter_country_code][1];

print "Two letters code: $two_letter_country_code<br>";
print "Three letters code: $three_letter_country_code<br>";
print "Country name: $country_name<br>";

function iptocountry($ip) {
    $numbers = preg_split( "/\./", $ip);
    include("ip_files/".$numbers[0].".php");
    $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
    foreach($ranges as $key => $value){
        if($key<=$code){
            if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;}
            }
    }
    if ($two_letter_country_code==""){$two_letter_country_code="unkown";}
    return $two_letter_country_code;
}
?>

作者: 网普科技     时间: 2005-3-3 08:14 PM    标题: 5:Getting the IP address of visitors, and displaying country and flag

Mase (from supercrab.com) has provided us the flags for most countries. In case you want to display flags in your page, download Flags file in the top of this page, and save all pictures to a folder named "flags" .

We have added a few lines of  code to script is previous example (in red) which allows showing country specific flags in our pages. This code will check whether a gif file containing the two country code exists in "flags" folder and displays it. In case the gif file in not in the folder, a default white flag is displayed. In case you have any of the missing flags, please send them to us.
CODE:  [Copy to clipboard]
<?
$IPaddress=$REMOTE_ADDR;
$two_letter_country_code=iptocountry($IPaddress);
  
include("IP_FILES/countries.php");
$three_letter_country_code=$countries[ $two_letter_country_code][0];
$country_name=$countries[$two_letter_country_code][1];

print "Two letters code: $two_letter_country_code<br>";
print "Three letters code: $three_letter_country_code<br>";
print "Country name: $country_name<br>";

// To display flag
$file_to_check="flags/$two_letter_country_code.gif";
if (file_exists($file_to_check)){
                print "<img src=flags/$file_to_check width=30 height=15><br>";
                }else{
                print "<img src=flags/noflag.gif width=30 height=15><br>";
                }

function iptocountry($ip) {
    $numbers = preg_split( "/\./", $ip);
    include("ip_files/".$numbers[0].".php");
    $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
    foreach($ranges as $key => $value){
        if($key<=$code){
            if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;}
            }
    }
    if ($two_letter_country_code==""){$two_letter_country_code="unkown";}
    return $two_letter_country_code;
}
?>

作者: steel     时间: 2005-3-9 01:13 PM
don't understand


作者: 网普科技     时间: 2005-3-9 01:25 PM


  Quote:
Originally posted by steel at 2005-3-9 01:13 PM:
don't understand


不懂就好好学习啊
只有不断地学习才会有所提高




欢迎光临 网普技术论坛 (http://bbs.netpu.net/) Powered by Discuz! 2.5