Page 1 of 1

get country code and say hi.. help

Posted: Sun Sep 26, 2004 8:16 pm
by fresh
hey, I'm looking to code a script for mulit national members, and what I would like to do is when they come to my page, they would trigger the script, and it would search the headers for the IP and translate it to the host and search the string for predifined country codes.. something like this:

Code: Select all

<?php
$cc0 = ".mt";
$cc1 = ".nl";
//etc..

$ip = gethostbyaddress('$REMOTE_ADDR');

//code that snakes the country code from the $ip variable  <-- help here

if($ip == $cc0) {
echo "Your logging in from Malta";
} else if($ip == $cc1) {
echo "Your logging in from the Netherlands";
}//etc..
?>
well hopefully you will get the picture with my example.. any help? thanks :D

Posted: Sun Sep 26, 2004 8:30 pm
by feyd
generally looking at everything after the last dot in the hostname (if one exists) will tell you which country.. you can readily find the TLD country list online.

Posted: Sun Sep 26, 2004 8:51 pm
by fresh
thanks, I actually figured it out and how to deal with mulitple dots, here's my code:

Code: Select all

<?php
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$chck = strstr($hostname, '.com');
$ccode = ".com";
if($chck == $ccode) {
echo "Your host is commercial: ".$chck." <-- see  ";
} else {
echo "I'm not too sure what your host is..  ";
}
?>
thanks :D

Posted: Mon Sep 27, 2004 4:09 am
by timvw
If you use GeoIP (look at the opensource stuff for free version) it goes like this:

Code: Select all

require_once('geoip.inc');

$address = $_SERVER['REMOTE_ADDR'];
$gi = geoip_open('GeoIP.dat', GEOIP_STANDARD);
$countrycode = geoip_country_code_by_addr($gi, $address);
geoip_close($gi);

echo $countrycode;

Posted: Mon Sep 27, 2004 8:59 pm
by fresh
hey that looks cool, whats it do run on the server and traceroute the IP? I don't have rights to install anything on the server (I don't think so) but it looks great.. thanks :D