网普技术论坛 网普科技  
» 游客:   网普主页 | 注册 | 登录 | 帮助
 

作者:
标题: 转:Country identification based on IP address 上一主题 | 下一主题
网普科技
网普管理员

网普科技人民公仆


积分 3080
发贴 2863
注册 2004-6-26
来自 网普科技
状态 离线
#1  转: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



天理路上甚宽,稍游心,胸中便觉广大宏朗;
人欲路上甚窄,才寄迹,眼前俱是荆棘泥涂。



网普科技,优质美国主机服务!
美国Linux主机,美国虚拟主机
支持PHP+MYSQL+cPanel+EMAIL
为用户负责,拒绝反动、赌博及色情内容! QQ:126818

发送QQ消息
2005-3-3 08:07 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部
网普科技
网普管理员

网普科技人民公仆


积分 3080
发贴 2863
注册 2004-6-26
来自 网普科技
状态 离线
#2  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.



天理路上甚宽,稍游心,胸中便觉广大宏朗;
人欲路上甚窄,才寄迹,眼前俱是荆棘泥涂。



网普科技,优质美国主机服务!
美国Linux主机,美国虚拟主机
支持PHP+MYSQL+cPanel+EMAIL
为用户负责,拒绝反动、赌博及色情内容! QQ:126818

发送QQ消息
2005-3-3 08:09 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部
网普科技
网普管理员

网普科技人民公仆


积分 3080
发贴 2863
注册 2004-6-26
来自 网普科技
状态 离线
#3  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.



天理路上甚宽,稍游心,胸中便觉广大宏朗;
人欲路上甚窄,才寄迹,眼前俱是荆棘泥涂。



网普科技,优质美国主机服务!
美国Linux主机,美国虚拟主机
支持PHP+MYSQL+cPanel+EMAIL
为用户负责,拒绝反动、赌博及色情内容! QQ:126818

发送QQ消息
2005-3-3 08:10 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部
网普科技
网普管理员

网普科技人民公仆


积分 3080
发贴 2863
注册 2004-6-26
来自 网普科技
状态 离线
#4  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;
}
?>




天理路上甚宽,稍游心,胸中便觉广大宏朗;
人欲路上甚窄,才寄迹,眼前俱是荆棘泥涂。



网普科技,优质美国主机服务!
美国Linux主机,美国虚拟主机
支持PHP+MYSQL+cPanel+EMAIL
为用户负责,拒绝反动、赌博及色情内容! QQ:126818

发送QQ消息
2005-3-3 08:11 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部
网普科技
网普管理员

网普科技人民公仆


积分 3080
发贴 2863
注册 2004-6-26
来自 网普科技
状态 离线
#5  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;
}
?>




天理路上甚宽,稍游心,胸中便觉广大宏朗;
人欲路上甚窄,才寄迹,眼前俱是荆棘泥涂。



网普科技,优质美国主机服务!
美国Linux主机,美国虚拟主机
支持PHP+MYSQL+cPanel+EMAIL
为用户负责,拒绝反动、赌博及色情内容! QQ:126818

发送QQ消息
2005-3-3 08:13 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部
网普科技
网普管理员

网普科技人民公仆


积分 3080
发贴 2863
注册 2004-6-26
来自 网普科技
状态 离线
#6  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;
}
?>




天理路上甚宽,稍游心,胸中便觉广大宏朗;
人欲路上甚窄,才寄迹,眼前俱是荆棘泥涂。



网普科技,优质美国主机服务!
美国Linux主机,美国虚拟主机
支持PHP+MYSQL+cPanel+EMAIL
为用户负责,拒绝反动、赌博及色情内容! QQ:126818

发送QQ消息
2005-3-3 08:14 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部
steel
网普超级版主





积分 246
发贴 246
注册 2005-3-6
来自 网普科技
状态 离线
#7  

don't understand





网普科技优质美国主机
2005-3-9 01:13 PM
查看资料  发送邮件  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部
网普科技
网普管理员

网普科技人民公仆


积分 3080
发贴 2863
注册 2004-6-26
来自 网普科技
状态 离线
#8  



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


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



天理路上甚宽,稍游心,胸中便觉广大宏朗;
人欲路上甚窄,才寄迹,眼前俱是荆棘泥涂。



网普科技,优质美国主机服务!
美国Linux主机,美国虚拟主机
支持PHP+MYSQL+cPanel+EMAIL
为用户负责,拒绝反动、赌博及色情内容! QQ:126818

发送QQ消息
2005-3-9 01:25 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部
茱莉娅
THE BODY SHOP美容顾问

茱莉娅美体小铺


积分 3080
发贴 2863
注册 2009-5-21
来自 茱莉娅美体小铺
状态 离线
#8  赞助商信息The body shop

茱莉娅美体小铺
茱莉娅美体小铺淘宝店
茱莉娅美体小铺


茱莉娅美体小铺淘宝店
2005-3-9 01:25 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部


可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题



论坛跳转:  




Powered by Discuz! 2.5 © 2001-2005 Comsenz Technology Ltd.
Processed in 0.012355 second(s), 5 queries, Gzip enabled
------------------------------------------------------------------------------
本论坛属网普科技交流与技术支持论坛!
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!
美国主机, 美国虚拟主机, cPanel+PHP+Mysql+Ftp+Email+Zend+GD2+国际域名支持
技术支持 QQ: 126818 EMail & MSN: support[AT]netpu.net
[ 联系我们 ] - [ 网普科技 ]