get country code and say hi.. help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

get country code and say hi.. help

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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;
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post 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
Post Reply