0

Following code is return 100.78.162.xxx ip and i wanted to know how we can identify the location country of that ip in php or android it self. Any help???

public static String getIPAddress(boolean useIPv4) {
        try {
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface intf : interfaces) {
                List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
                for (InetAddress addr : addrs) {
                    if (!addr.isLoopbackAddress()) {
                        String sAddr = addr.getHostAddress().toUpperCase();
                        boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); 
                        if (useIPv4) {
                            if (isIPv4) 
                                return sAddr;
                        } else {
                            if (!isIPv4) {
                                int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                                return delim<0 ? sAddr : sAddr.substring(0, delim);
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) { } // for now eat exceptions
        return "";
    }
String ip = Utils.getIPAddress(true);
Sufian
  • 6,405
  • 16
  • 66
  • 120
mukesh patel
  • 161
  • 1
  • 1
  • 16
  • You need a database that maps IPs to countries. The keyword here is `GeoIP`. Just google for it... – bidifx Jun 23 '14 at 07:23
  • Please take a look at similar issue below: [http://stackoverflow.com/questions/7766978/geo-location-based-on-ip-address-php][1] [1]: http://stackoverflow.com/questions/7766978/geo-location-based-on-ip-address-php – Tikky Jun 23 '14 at 07:23
  • This might be useful: http://stackoverflow.com/questions/6437172/how-to-know-geographic-location-from-an-ip-address – Badrul Jun 23 '14 at 07:27
  • @Tikky : it is right solution who want to retrieve ip using only php but i want ip in android and pass this ip in php to check the region but android device retrun ip 100.78.162.232 form which is invalid so how can we resolve this issue. – mukesh patel Jun 23 '14 at 07:52
  • @bidifx Yes you are right but it is invalid ip for geoip. – mukesh patel Jun 23 '14 at 07:57
  • Okay, as it's a mobile device its likely that some NAT (Network Address Translation) is involved. So the device itself cannot determine it's internet-facing IP without help from the outside. You could use some PHP that determines the actual IP. What is the reason you don't want to do that? – bidifx Jun 23 '14 at 08:12
  • Anyway, db-ip.com says it's `Marina del Rey`, correct? – bidifx Jun 23 '14 at 08:14
  • @bidifx sorry for late reply, hmm db-ip says it marina del ray. but i am using this device in pune india. so might be it returned device ip address because its manufacturer in us. i am using moto-g handset – mukesh patel Jun 23 '14 at 08:37
  • @mukeshpatel check out my previous comment. what's the devices internet-facing ip? from the device itself you'll only get the NAT-IP. – bidifx Jun 23 '14 at 12:18
  • @bidifx I am getting this ip 100.78.162.232 and it return Marina del Rey so how can i determine the actual ip using php?? – mukesh patel Jun 23 '14 at 12:29

1 Answers1

0

You can use services like ip2location to get the location based on ipAddress.Its better be incorporated on the server side and expose this service using a webservice. Or you can use services like location_api for consuming directly from other service.

NOTE : You can also use GPS signals (if you are allowed to use this) to get the location accurately.

Akshay Deo
  • 528
  • 1
  • 6
  • 22