I wanted to do the same with mysoftware
( WITHOUT USING 3rd PARTY WEBSITES OR DATABASES! This is done on the fly by your OWN server )
After a while, i came up with this:
Here goes,
1) Your php script needs access to run the a whois command.
2) Your server needs to be linux.
You will need to have this snippet on the page your visitors land on.
Code:
<?php
$ip = $_SERVER['REMOTE_ADDR']; // Get users IP
$country = exec("whois $ip | grep -i country"); // Run a local whois and grep for the word country.
$country = strtolower($country); // lowercase everything.
// Get rid of unwanted whois response.
$country = str_replace("country:", "", "$country"); // Clean stuff unwanted from whoisis
$country = str_replace("network:country-code:", "", "$country"); // Clean stuff unwanted from whoisis
$country = str_replace("network:organization-usa", "", "$country"); // Clean stuff unwanted from whoisis
// End get rid of.
$country = str_replace(" ", "", "$country"); // get rid of any spaces.
echo $country; // this will now be a 2char country code, IE: gb
?>
You can then use flag icons from somewhere like famfamfam to display a flag icon based on the country code.
ie:
Code:
<?php
echo '<img alt="'.$country.'" src="Images/flags/png/'.$country.'.png" width="16" height="11" /> <b>('.$country.')</b>';
It works very well for me where i use it to map ips that have visited the site to a country and put a flag next to it.
It gets the country for an IP 90% to 95% of the time, Others will just come back as empty vars ( so work in a if var == "" then flag = unknown. )
Its more accurate that paid databases as whois is updated by the isp themselves.
Hope this helps anyone else out there.
This code can be changed to take any data from a whois. ( ie the ISP name, town location etc )
The whole point of this is so you wont need to rely on a 3rd party database or some other 3rd party website.
Whois is far more reliable.
Thanks
G