Jump to content

Finding the accurate zip code based on IP Address


Recommended Posts



I am building an application in my page where I need to add geolocation functionality and I need the visitors zip code in return, like say within 100miles. I have some sample source code but returning only Country, States and City.
Could anyone please help? Any input is greatly appreciated.
Link to comment
Share on other sites

Use a script to calculate the geolocation (ipgetinfo is helpful), something among the lines of <?php function geo() { $ip = $_SERVER['REMOTE_ADDR']; $url = 'http://www.ipgetinfo.com/index.php?ip='.$ip.''; $gc = fopen($url, "r"); $doc = new DOMDocument(); $fc = stream_get_contents($gc); $doc->load($gc); $doc->getElementsByClassName('...'); ... } ?>.

The script isn't completed yet, as I cannot get on a PC anytime soon, I have to cancel the script early, write a script that calculates the zip out of the geolocation data ipgetinfo provides. Hope this helped.

Edited by Irate
Link to comment
Share on other sites

Not accurately, no. You might get the country, and the general vicinity (city) correct most of the time. But, the ZIP-code: No chance to get the correct one with any degree of accuracy.


Contrary to Hollywood, an IP-address isn't tied to a geographical location, but to businesses. Internet Service Providers. Whom then, in turn, assign these to their customers based upon various internal rules. For the big national ISPs, this might mean that a single IP-address could be used anywhere in the whole country, depending upon who requested an IP-address first when that particular IP-address was available. Smaller, or more sectioned up ISPs could give you a smaller geographical area, possibly down to city-level. Though, not much more than that, and a city has multiple ZIP-codes after all.

That's not even considering the vast areas with a minimal population, such as the countryside.


The only way to get the accurate ZIP-code for any given IP-address, is to actually look it up in the ISPs registers. Which means comparing their DHCP logs with their customer registers, something which would be illegal without a court order.

Edited by Christian F.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.