简体   繁体   中英

IP API lookup optimization in php

I'm trying to limit my number of requests to an IP geolocation API,I want to know if this will accurately determine user country;

My IP table

Id |-----ip-----| Country|

1 | 1.194.121.200 | USA


If I have the above in my Database, can I use same country for an IP (1.194.208.105) having same integers in their first two octets, to avoid another API request?

Not reliably, no.

  1. IP subnets work by bitmasks which only occasionally align with dotted-quad octets.
  2. IP allocations change.
  3. Because of IPv4 scarcity blocks are regularly being broken into smaller and smaller pieces allocations can change drastically in terms of both network size and geography.

Eg: A few years ago Azure had problems because they bought a block of IPs from LACNIC [south america] to use in their US-based cloud. This caused all sorts of havoc because people using stale IP registration/geolocation data started complaining that they didn't want their data located outside of the US.

By all means cache the per-IP results for a reasonable period, but you're not going to be able to cache by netblock unless either your API provider gives you that data, or you want build your own IP geolocation service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM