phpsane Posted December 1, 2018 Share Posted December 1, 2018 Php Whizzes! Need to grab geo ip. Why these 2 show data differently ? http://www.geoplugin.net/json.gp?ip=19.117.63.253 http://www.geoplugin.net/php.gp?ip=19.117.63.253 Which one you like ? I like 1st one. Anyway, is this code any good ? Will it misfire ? <?php //https://stackoverflow.com/questions/12553160/getting-visitors-country-from-their-ip function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) { $output = NULL; if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) { $ip = $_SERVER["REMOTE_ADDR"]; if ($deep_detect) { if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) $ip = $_SERVER['HTTP_CLIENT_IP']; } } $purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose))); $support = array("country", "countrycode", "state", "region", "city", "location", "address"); $continents = array( "AF" => "Africa", "AN" => "Antarctica", "AS" => "Asia", "EU" => "Europe", "OC" => "Australia (Oceania)", "NA" => "North America", "SA" => "South America" ); if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) { $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip)); if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) { switch ($purpose) { case "location": $output = array( "city" => @$ipdat->geoplugin_city, "state" => @$ipdat->geoplugin_regionName, "country" => @$ipdat->geoplugin_countryName, "country_code" => @$ipdat->geoplugin_countryCode, "continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)], "continent_code" => @$ipdat->geoplugin_continentCode ); break; case "address": $address = array($ipdat->geoplugin_countryName); if (@strlen($ipdat->geoplugin_regionName) >= 1) $address[] = $ipdat->geoplugin_regionName; if (@strlen($ipdat->geoplugin_city) >= 1) $address[] = $ipdat->geoplugin_city; $output = implode(", ", array_reverse($address)); break; case "city": $output = @$ipdat->geoplugin_city; break; case "state": $output = @$ipdat->geoplugin_regionName; break; case "region": $output = @$ipdat->geoplugin_regionName; break; case "country": $output = @$ipdat->geoplugin_countryName; break; case "countrycode": $output = @$ipdat->geoplugin_countryCode; break; } } } return $output; } echo ip_info("119.30.32.215", "Country"); ?><br><?php echo ip_info("119.30.32.215", "Country Code"); ?><br><?php echo ip_info("119.30.32.215", "State"); ?><br><?php echo ip_info("119.30.32.215", "City"); ?><br><?php echo ip_info("119.30.32.215", "Address"); ?><br><?php print_r(ip_info("119.30.32.215", "Location")); // Array ( [city] => Menlo Park [state] => California [country] => United States [country_code] => US [continent] => North America [continent_code] => NA ) ?> What you say ? Lemme see your sample code. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/ Share on other sites More sharing options...
requinix Posted December 1, 2018 Share Posted December 1, 2018 6 hours ago, phpsane said: Why these 2 show data differently ? http://www.geoplugin.net/json.gp?ip=19.117.63.253 http://www.geoplugin.net/php.gp?ip=19.117.63.253 Look at the URLs. Make an educated guess. I know that may be hard for you to do but even you should be able to tell that the two are different, and if you can see what the difference is then maybe, just maybe, you might have a clue. 1 Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/#findComment-1562559 Share on other sites More sharing options...
phpsane Posted December 3, 2018 Author Share Posted December 3, 2018 On 12/2/2018 at 2:25 AM, requinix said: Look at the URLs. Make an educated guess. I know that may be hard for you to do but even you should be able to tell that the two are different, and if you can see what the difference is then maybe, just maybe, you might have a clue. Ha! Ha! I knew both files were using different languages (based on their file extensions) and don't needed to be educated to figure that out. It's just I don't know this json thing and couldn't figure-out why they didn't just add each entry on a line by themselves on the php one like the json one. Why switch langs just to have each entry on lines by themselves ? What's the real significance ? Unless ofcourse the json one is doing some other tasks in the background which an open human eye fails to catch. Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/#findComment-1562581 Share on other sites More sharing options...
requinix Posted December 3, 2018 Share Posted December 3, 2018 If you think that the only difference between those outputs was whether stuff went on separate lines or not then you need to get your eyes checked. 1 Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/#findComment-1562585 Share on other sites More sharing options...
Barand Posted December 3, 2018 Share Posted December 3, 2018 I couldn't help noticing this example of coding efficiency $address = array($ipdat->geoplugin_countryName); if (@strlen($ipdat->geoplugin_regionName) >= 1) $address[] = $ipdat->geoplugin_regionName; if (@strlen($ipdat->geoplugin_city) >= 1) $address[] = $ipdat->geoplugin_city; $output = implode(", ", array_reverse($address)); Build the array in the wrong order then reverse it to output in correct order 1 Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/#findComment-1562589 Share on other sites More sharing options...
phpsane Posted December 13, 2018 Author Share Posted December 13, 2018 On 12/3/2018 at 11:20 PM, Barand said: I couldn't help noticing this example of coding efficiency $address = array($ipdat->geoplugin_countryName); if (@strlen($ipdat->geoplugin_regionName) >= 1) $address[] = $ipdat->geoplugin_regionName; if (@strlen($ipdat->geoplugin_city) >= 1) $address[] = $ipdat->geoplugin_city; $output = implode(", ", array_reverse($address)); Build the array in the wrong order then reverse it to output in correct order Ok. I quit.I'll have it your way then.Now, how are you gonna make sure that your members are fromn the country they say they are from ?Say, a guy from USA registers at your membership site (reg.php) and the reg.php prompts the user for his country and he selects Australia. Now, how you gonna force him to select his country truthfully ?Have you seen when you try visiting paypal.com then if you're from UK then it redirects you to paypal.co.uk ? Here, the user is not given any choice to select his country. Paypal auto detects the user's country. No surprise it would put an entry for that user in their mysql tbl "UK" as the user's country. Let's try doing something like that. Shall we ? And so, how to do it ?How-about a sample code from your end ? @requinix, You're welcome to show a code sample how you'd do things. Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/#findComment-1562793 Share on other sites More sharing options...
requinix Posted December 13, 2018 Share Posted December 13, 2018 3 minutes ago, phpsane said: Now, how are you gonna make sure that your members are fromn the country they say they are from ? You can't. 3 minutes ago, phpsane said: And so, how to do it ? Don't ask questions you already know the answers to. Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/#findComment-1562795 Share on other sites More sharing options...
phpsane Posted December 13, 2018 Author Share Posted December 13, 2018 (edited) 3 minutes ago, requinix said: You can't. Don't ask questions you already know the answers to. Hey ? I don't know the code how to get php to detect the user's country correctly. Someone told me geoip is not the way to do it as it can be fooled. So, let us see how you folks would do things. Edited December 13, 2018 by phpsane Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/#findComment-1562797 Share on other sites More sharing options...
requinix Posted December 13, 2018 Share Posted December 13, 2018 GeoIP is the best you can get in general. For mobile users you can ask them to allow your site to read their GPS location. With Javascript. 1 Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/#findComment-1562801 Share on other sites More sharing options...
phpsane Posted December 13, 2018 Author Share Posted December 13, 2018 29 minutes ago, requinix said: GeoIP is the best you can get in general. For mobile users you can ask them to allow your site to read their GPS location. With Javascript. Ok then. Might aswell sticck to geoip. However, Barand thinks the code on my original post is clumsy. Any chance you can weed-out the chaff and show me a better way to do it ? Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/#findComment-1562804 Share on other sites More sharing options...
phpsane Posted December 13, 2018 Author Share Posted December 13, 2018 On 12/3/2018 at 11:20 PM, Barand said: I couldn't help noticing this example of coding efficiency $address = array($ipdat->geoplugin_countryName); if (@strlen($ipdat->geoplugin_regionName) >= 1) $address[] = $ipdat->geoplugin_regionName; if (@strlen($ipdat->geoplugin_city) >= 1) $address[] = $ipdat->geoplugin_city; $output = implode(", ", array_reverse($address)); Build the array in the wrong order then reverse it to output in correct order Barand & Requinix, I found it here: https://stackoverflow.com/questions/12553160/getting-visitors-country-from-their-ip What you think now ? How-about trying to fix it since you deem it is not worthy at current state ? Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/#findComment-1562805 Share on other sites More sharing options...
phpsane Posted December 18, 2018 Author Share Posted December 18, 2018 On 12/14/2018 at 2:55 AM, requinix said: GeoIP is the best you can get in general. For mobile users you can ask them to allow your site to read their GPS location. With Javascript. So you reckon the code on my original post is good to go ? Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/#findComment-1562899 Share on other sites More sharing options...
requinix Posted December 18, 2018 Share Posted December 18, 2018 The code quality is unimpressive but at least it seems like it will do the job. Quote Link to comment https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/#findComment-1562904 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.