Archive for IP Addressing

Getting the location from an IP address

If you want to retrieve information like the city, state, and country of a visitor from their IP address, so that you can customize your web page according to their location.  There a good and reliable way to do this in PHP. You are using JavaScript for client-side scripting, PHP for server-side scripting, and MySQL for the database.

You could download a free GeoIP database and lookup the IP address locally, or you could use a third party service and perform a remote lookup. This is the simpler option, as it requires no setup, but it does introduce additional latency.

$ curl
  "ip": "",
  "hostname": "",
  "loc": "37.385999999999996,-122.0838",
  "org": "AS15169 Google Inc.",
  "city": "Mountain View",
  "region": "CA",
  "country": "US",
  "phone": 650

Here’s a PHP example:

$details = json_decode(file_get_contents("{$ip}/json"));
echo $details->city; // -> "Mountain View"

You can also use it client-side. Here’s a simple jQuery example:

$.get("", function(response) {
}, "jsonp");


Location detecting tecniques for IP addresses

There are several things that allow you to create such a db, if you invest enough time:

  1. Use the databases provided by regional and local registries to find an IP’s owner.
  2. Many ISPs use a naming schema that allows you to locate the user. Sometimes you can even read the city name in plain text if you do a reverse-DNS lookup. Sometimes it is more cryptic. For example I currently have , and I have no idea that the naming scheme is..
  3. Use a traceroute. If you can’t identify the location of a user, you can still find out the location of its uplink

IP address calculation rule

IP address calculation rule

How to get Block size of given subnet mask

Subnet mask -256

Example:-   Given subnet mask =

Block Size will be  256 – 248 = 8

Usable IPs will be    8 – 2 = 6

Because first IP will be used for Network ID and Last IP will be used for Broadcast

How to get Block Size of given Bit

32 Bit – given Bit = x bit     Block Size   (2) the power     x bit

For Example:-  Given IP address range is /29

Block Size will be    32 bit – 29 bit = Block size 3 bit              (2)3 = 2x2x2 = 8

So there will be 8 subnets    To get first IP subnet Block 256 / Block Size

For Above Block Example    256 /8 = 32

1-      Block   0 – 31

2-      Block   32- 63

3-      Block  64 –95

4-       Block 96 – 127

5-      Block 128 – 159

6-      Block 160 – 191

7-      Block 192 – 223

8-      Block 224 – 256