RON_ron Posted August 30, 2012 Share Posted August 30, 2012 I need to implement a way to check if the web user belongs to 2 selected countries. can the IP address be used to identify the country?Any suggestions are highly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/267819-website-visitor/ Share on other sites More sharing options...
Christian F. Posted August 30, 2012 Share Posted August 30, 2012 Not reliably, no. You can use the IP to determine which country the ISP is registered in, but that doesn't necessarily mean that the user belong to that country as well. After all, people go on holidays as well as using proxies, and nothing's to say that the geolocation database of your provider is correct. I've had my IP identified as belonging to the Netherlands, quite a few times, and others no result at all. Best way is to ask the user, and then save the result in the database or a cookie. Quote Link to comment https://forums.phpfreaks.com/topic/267819-website-visitor/#findComment-1373987 Share on other sites More sharing options...
QuickOldCar Posted October 7, 2012 Share Posted October 7, 2012 As Christian said, not reliably. http://www.maxmind.com/en/geolocation_landing http://www.geoplugin.net can try this $ip = $_SERVER['REMOTE_ADDR']; //if running this code locally, to get a result if($ip == $_SERVER['SERVER_ADDR']){ $ip = $_SERVER['SERVER_NAME']; } echo $ip."<br />"; $geolocation = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip)); $geo_city = trim($geolocation['geoplugin_city']); $geo_country = $geolocation['geoplugin_countryName']; $location = $geo_city.",".$geo_country; echo "Location: ".$location."<br />"; What you do with their city,country info would be up to you, save it to a user session or database Quote Link to comment https://forums.phpfreaks.com/topic/267819-website-visitor/#findComment-1383396 Share on other sites More sharing options...
QuickOldCar Posted October 7, 2012 Share Posted October 7, 2012 (edited) It seems the code had a hard time displaying this geoplugin.net/php.gp?ip='.$ip Edited October 7, 2012 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/267819-website-visitor/#findComment-1383397 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.