Track User’s Real IP Address

Last updated on August 19th, 2017 by Paritosh Pandey

We can get the IP (Internet Protocol) address of any visitor of your website or blog by using PHP. Tracking the IP address is very important requirement for many applications or scripts where we store the users or visitors details. For security reason we can store IP address of our website visitors who are doing any purchases or doing any type of online transaction. We can use the IP to find out geographical location of the visitors. Sometime based on the IP we can redirect the browser to different areas of the site.

What is my IP Address

Each time when we connect to an Internet through our ISP ( Internet Service Provider ) we get allotted one IP from a pool of IP address available to our ISP. This pool of IP were assigned to your ISP by IANA ( Internet Assigned Numbers Authority ). IANA is a department of ICANN  (Internet Corporation for Assigned Names and Numbers).

So from time to time your IP keep changing, it is like using a hired Taxi each day. Your cab operator will give you a Taxi each time you ask with a different number plate.

There are lots of IP tracker available out there. You can use as well to find your IP address, just navigate google’s page and search for “my ip” keyword.


You may also like

Why it is required to track user’s IP address

There are many positive reasons available which inspire owners to track their visitors IP, such as:

  • analytics (to know how your website is performing in other countries)
  • users or visitors details if any security problem occurs
  • for knowing which Geolocation is performing well in the terms of getting traffic.

However Cybercrime is increasing day by day in the age of Internet. It is highly required you to know all about you clients or visitors of website or blog.

How to get user’s IP Address

We all know that getting the IP of the visitors is a quite simple job using PHP, even in other programming languages. But are you sure that you are getting the original IP of the user?

In PHP, $_SERVER['REMOTE_ADDR'] is used to get the user’s IP. But what happen if any user which currently surfing your website from USA via proxy server of Germany. In this case $_SERVER[‘REMOTE_ADDR’] will return ip of the Germany rather than ip of USA.

We can use the following environment variables to get visitor’s IP:

With Proxy detection



No Proxy detection



Full combined code is here

if (!empty($_SERVER['HTTP_CLIENT_IP'])){
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){


If you are going to save the user’s IP address into a database as a string, make sure you have space for at least 45 characters long. Because IPv4 is being replaced by a newer IPv6, it has maximum length of 39 characters and IPv4-mapped IPv6 address has maximum length of almost 45 characters. More and more servers are getting the IPv6 now and these addresses are larger than the older IPv4 addresses.

Leave a Reply

Your email address will not be published. Required fields are marked *