lynxus Posted August 16, 2009 Share Posted August 16, 2009 Hi all, This isnt a question, Rather its a how to as ive spent ages trying to figure out how to do this without the need of using someone elses database ( that they want money for! ) 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. <?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: <?php echo '<img alt="'.$country.'" src="Images/flags/png/'.$country.'.png" width="16" height="11" /> <b>('.$country.')</b>'; 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 www.imsupporting.com Link to comment https://forums.phpfreaks.com/topic/170507-solved-get-visitors-country-code/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.