My Programming Tutorials

My Programming Tutorials

Track User’s Real IP Address

Last updated on by , 4 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

Author Info

Paritosh Pandey

He loves Technology

Advertisement

4 responses to “Track User’s Real IP Address”

  1. bilim says:

    I was curious if you ever considered changing the layout of your website?
    Its very well written; I love what youve got to
    say. But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having
    1 or two pictures. Maybe you could space it out better?

    • Jo Nas says:

      I guess you are referring to an older version of this website, as the current one seems very modern and clean to me.

  2. Good day I am so excited I found your webpage, I really found you by error,
    while I was looking on Bing for something else, Anyways I am
    here now and would just like to say thanks for a marvelous post and a all round thrilling blog (I
    also love the theme/design), I don’t have time to go
    through it all at the minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read
    much more, Please do keep up the awesome work.

  3. Ӏ think the admin of tһis web page is in fact working hard in favor of his web
    pаge, as here every material is quality based information.

Leave a Reply

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