Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Country Name or Code for a Referer

Status
Not open for further replies.

tyhand

Programmer
Jul 3, 2002
186
US
Hey all,

Anyone know how I can get the country name or code for a referer using php?

I wrote the following code to get the country name where a referer is from, but I keep getting a FATAL ERROR.

// get country name location of host
$refer = parse_url($_SERVER['HTTP_REFERER']);
$host = $refer['host'];
$loc = geoip_country_code_by_name($host);

I'm using this code for both PHP 4 & 5, but am having no luck.

Any help is greatly appreciated. Thanks!

- tyhand
 
Did you install the PECL Extensions? you need thme to be able to use the geoip_country_code_by_name() function.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hey Vacunita,

Thanks for the help.

Actually, I'm on a shared host and I don't know if the extensions have been configured or even installed.

Any alternatives to getting country code or name w/ php?

In the meantime, I'll look into the pecl. Thanks again!

- Tyhand
 
sure. just use any of the available web services. try googling : geo lookup by IP php

 
What was the fatal error reported by PHP?

Maybe try something like this.
Code:
if( function_exists('geoip_country_code_by_name') )
{
  $refer = parse_url((string)$_SERVER['HTTP_REFERER']);
  if( isset($refer['host']) )
  {
    $host = $refer['host'];  
    $loc = geoip_country_code_by_name($host);
    echo 'Location: '.$loc;
  }
  else
  {
    echo 'Variable \'$refer[\'host\']\' is not set';
  }
}
else
{
  echo 'Function \'geoip_country_code_by_name()\' does not exist';
}

-a6m1n0

 
A logn time ago the php.net site used to give you a link to a csv file that contained ip blocks against country, it uses this to select the mirror to use.
I wonder if that file is still avaiable?, for data which is essentialy public it seems qute hard to get to.
 
Hi

You mean this ?
MyPHP.net said:
[navy]Your country[/navy]

The PHP.net site and mirror sites try to detect your country using the Directi Ip-to-Country Database. This information is used to mark the events in your country specially and to offer close mirror sites if possible on the download page and on the mirror listing page.

We were unable to detect your country
My PHP.net | Your country

( By the way, personally I used ip-to-country.csv.zip. )

Feherke.
 
feherke- your the main man !
are the numbers in the csv the ip address just without the dots ???
 
Hi

Someone mentioned "play" ? ;-)
Code:
function getcountry($ip,$nameonly=false)
{
  $ipfile='path/to/ip-to-country.csv';
  $long=sprintf('%u',ip2long($ip));
  $result=NULL;

  $FIL=fopen($ipfile,'r');
  while ($one=fgetcsv($FIL)) if ($long>=$one[0] && $long<=$one[1]) {
    $result=$nameonly?$one[4]:array_splice($one,2);
    break;
  }
  fclose($FIL);

  return $result;
}

list($tld,$code,$name)=getcountry('216.45.19.33');

$name=getcountry('216.45.19.33',true);

Feherke.
 
i use net/geoIP from pear for this stuff.

this is the code snip i use

Code:
require_once "Net/GeoIP.php";
try {
	$geoip = @Net_GeoIP::getInstance("./GeoLiteCity.dat");
	try {
		$location = $geoip->lookupLocation($ipAddress);
		$city= isset($location->city) ? $location->city : 'not set';
		$countryCode = isset($location->countryCode) ? $location->countryCode: 'not set';
		$countryName = isset($location->countryName) ? $location->countryName: 'not set';
		$state = isset($location->region) ? $location->region: 'not set';;
	} catch (Exception $e) {
		$city = $countryCode = $state = $countryName = 'not set';
	}
} catch (Exception $e) {
	$city = $countryCode = $state = $countryName = 'not set';
}
 
yup. but as you'll see it's in a try ... catch block, so i'm not so much suppressing an error as trapping it! so i hope i'm forgiven!

it's used as an automated spam script. i track all comment submissions to a client's site and one of the heuristics we use is the general locale and time of day. it's not a great measure (although blocking the entirety of russia, baltic states and china resulted in no perceivable loss of business and a mammoth reduction in spam).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top