Sitekeepers - Webmaster's blog

Wednesday, August 24, 2005

Ip to Country

I found an ip to country PHP function on www.freestuff.gr and i would like to share with you.

[code] //What country
/**
* @return void
* @param integer $ip The IP of the visitor
* @desc Searches the server and gets the country code suplied to this ip.
*/
function country(){
$ip=$_SERVER['REMOTE_ADDR'];
$nextServer = "whois.ripe.net";
if($nextServer){
$buffer = "";
if(! $sock = fsockopen($nextServer, 43, &$num, &$error, 10)){
unset($sock);
}
else{
fputs($sock, "$ip\n");
while (!feof($sock))
$buffer .= fgets($sock, 10240);
fclose($sock);
}
}
$buffer = str_replace(" ", " ", $buffer);
$msg = nl2br($buffer);
$posun = strpos ($msg, "0.0.0.0 - 255.255.255.255");
if ($posun>0){
$rest="unknown";
$msg=" ";
}
$posus = strpos ($msg, "Florida");
if ($posus>0){
$rest="US";
$msg="US";
}else{
$pos = strpos ($msg, "country:");
if ($pos>0){
$rest = substr ($msg, $pos+44, 2);
}
$rest=str_replace(" ", "", $rest);
}
$this->country=$rest;
}[/code]

0 Comments:

Post a Comment

<< Home