Track User’s Real IP Address

Last updated on November 2nd, 2017 by Paritosh Pandey, no comments


Hello Readers! In this article, we are about to learn how you can capture your website visitor’s Real IP Address using PHP. First of all, we need to know what actually an IP address is?

Track Real IP address of website visitors using PHP

Following snippet of PHP code would help you to catch the visitor’s real IP address even they are surfing your website through a proxy.


if(!empty($_SERVER['HTTP_CLIENT_IP'])){
    $ip=$_SERVER['HTTP_CLIENT_IP'];
} else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){

    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];

} else {

    $ip=$_SERVER['REMOTE_ADDR'];

}

I’m sure you are willing to know everything about above snippet of codes, and what each code do? No worry I am going to explain everything about it further.

What is an IP Address?

IP stands for Internet Protocol. An IP is a group of numeric digits separated by dots. IP gets allotted to every machine (computer device) which is connected to computer networks which use the Internet Protocol (TCP/IP) for their communications.

An IP is a unique identification that almost every computing device (personal computers, smartphones, tablets etc.) have to identify themselves in an IP-based network to communicate with other computing devices.

In an IP network, every device must have their own and unique IP address. Similar to Home or any physical location or place which have their own physical address.

Tow types of IP addresses are being used today in abundance, these are IPv4 and IPv6.

IPv4

The IPv4 is made up of 4 groups of digits, which are divided by 3 dots, each group can be between 0 – 255. For example, IP address of MPT(www.myprogrammingtutorials.com) is 198.54.116.175. This is the unique identification number which is used to identify MPT Blog on the Internet.

IPv6

An IPv6 address format is totally different than the format of IPv4. It is made up of 4 hexadecimal digits separated by colons (“:”). It looks something like “2405:204:e201:df32:1d15:8e4a:8c61:65b“.

Why it is required to track user’s IP address

Tracking the IP address is a very important requirement for any 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 performing any type of online transaction. We can use the IP to find out the geographical location of the visitors. Sometimes based on the IP we can redirect the browser to different areas of the site.

I’m sure you have heard about Google Analytics. It is web analytics service provided by The Google launched in November 2005. It uses IP to track the visitors.

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

  • Traffic analytics (to know how your website is performing in other countries)
  • user’s or visitor’s 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 the Internet. It is highly required you to know all about your clients or visitors of your website or blog.

How to get User’s IP Address

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

my-ip

But in this article, we are discussing how to fetch user’s IP address programmatically. The programming language we are going to use here is PHP. PHP is a Server Side scripting language which is widely used to develop web applications.

However, every server-side language has their own inbuilt methods to capture an IP address. PHP uses $_SERVER['REMOTE_ADDR'] variable to store an 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 happens if any user which currently surfing your website from the USA via the 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, this would return the real IP address of visitors.


$_SERVER['HTTP_X_FORWARDED_FOR'];

 

No Proxy detection, this would return the proxy IP address.


$_SERVER['REMOTE_ADDR'];

Note:

If you want to store IP address in a database, I highly recommend to store it as a string with space of at least 45 characters. Because IPv6 is taking place of IPv4, as it is a newer version. IPv6 contains a maximum length of 39 characters and IPv4-mapped IPv6 address contains a maximum length of 45 characters.

You may also like


Leave a Reply

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