Hi, i have a banner area on my website where i would like to display an image. I would like to display a different image in the banner area, depending on a users' location. I have 4 different images: 01 - usa_banner.jpg (default) 02 - europe_banner.jpg 03 - uk_banner.jpg 04 - australia_banner.jpg If a user views the site from a country outside of the top 4 areas, then i would like to display the default usa_banner.jpg image. I have created my site using codeigniter. What is the best way to work out the IP ranges etc. Any advice on this would be greatly appreciated. Thanks in advance...
Hi, You can use below code. <?php // PHP file. $ip = $_SERVER['REMOTE_ADDR']; $country = exec("whois $ip | grep -i country"); // Run a local whois and get the result back $country = strtolower($country); // Make all text lower case so we can use str_replace happily // Clean up the results as some whois results come back with odd results, this should cater for most issues $country = str_replace("country:", "", "$country"); $country = str_replace("Country:", "", "$country"); $country = str_replace("Country :", "", "$country"); $country = str_replace("country :", "", "$country"); $country = str_replace("network:country-code:", "", "$country"); $country = str_replace("network:Country-Code:", "", "$country"); $country = str_replace("Network:Country-Code:", "", "$country"); $country = str_replace("networkrganization-", "", "$country"); $country = str_replace("networkrganization-usa", "us", "$country"); $country = str_replace("network:country-code;i:us", "us", "$country"); $country = str_replace("eu#countryisreallysomewhereinafricanregion", "af", "$country"); $country = str_replace("", "", "$country"); $country = str_replace("countryunderunadministration", "", "$country"); $country = str_replace(" ", "", "$country"); ?>
there are some location api services in web that can do this with more precision. I dont know if i can give links, but google is your friend
<?php $ip = $_SERVER['REMOTE_ADDR']; $api_key = "fa21adbc3ead7eda75b0192aa6b296e7ddba9a6e71c32018ae1938289241af58"; $data = file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key='".$api_key."'&ip=74.125.45.100&format=json"); $country_code = json_decode($data); $code = $country_code->{'countryCode'}; echo '<img src="'.$code.'.jpg"/>'; ?>
I use the maxmind geolite. Good stuff you can have your server cron a monthly update of the free database and then use it locally on your server. it just becomes a call into your own framework to get a country code.
Uhm, ever heard of str_ireplace? Just saying. Much less you convert it to lower-case, then run upper case compares?!? Also, you don know you can used arrays for that, right? And what the devil is with the double quotes around the $country var for nothing?!? <?php $ip = $_SERVER['REMOTE_ADDR']; $country = exec("whois $ip | grep -i country"); // Run a local whois and get the result back $country = str_ireplace( array( 'country:', 'network:country-code:', 'network:organization-', 'network:organization-usa', 'network:country-code;i:us', 'eu#countryisreallysomewhereinafricanregion', 'countryunderunadministration', ' ' ), array( '','','',us','us','af','' ), $country ); Code (markup): Should be functionally identical. NOT that I would use exec to do such a thing -- since that wouldn't be cross platform and may be blocked on some hosts. (both the exec and the whois) ... and since grep is doing the extraction, wouldn't just detecting : on the line be faster? I'll have to play with this