Skip to content

How To Get IP Address Of The Client Using PHP?

In this article we are going to show you how you can get the correct IP address of the client. Well, it is very important to know the IP address of the client or visitor in order to prevent spams or some other illegit activities that a visitor might do.

To overcome such issues, we must know the IP address of the client. Before knowing the way to get the address, let’s give you an idea that what is an IP address. Go through this post and learn deeply about IP addresses collecting using php or you may join our php training program to learn other advanced features in php programming.

An IP(Internet Protocol) address is a set of numbers separated by full stops which determines the identity of the host and the location address. An Internet Protocol address looks like this 192.14.267 or 2004:db8:a0b:1357:0:436:8:2 depending on the IP versions.

WAYS TO GET IP ADDRESS OF THE CLIENT USING PHP

(1) Using $_SERVER[‘REMOTE_ADDR’] Superglobal Variable 

One way to get the Internet Protocol address of a client is using the superglobal variable $_SERVER[‘REMOTE_ADDR’]. It is the easiest way to retrieve the Internet Protocol address of the visitor.

However, this function has limitations. It cannot access the actual Internet Protocol address of the client when he/she is using a proxy server.

Instead of fetching the actual Internet Protocol address of a user client, it will fetch the IP address of the proxy server. So, the use of this superglobal variable is limited but it can be utilized by using it with other variables to fetch actual results which we will discuss later in this article.

Example : A simple example of getting an IP address using $_SERVER[‘REMOTE_ADDR’] superglobal variable is shown below.

php-getip-1024x519
php-getip2-1024x493

Output : You will get the output of the above php code as shown below.

(2) Using $_SERVER[‘HTTP_CLIENT_IP’] and $_SERVER[‘HTTP_X_FORWARDED_FOR’]Superglobal Variables 

The another way to retrieve the Internet Protocol address of a user client in php is using the superglobal variables $_SERVER[‘HTTP_CLIENT_IP’] and $_SERVER[‘HTTP_X_FORWARDED_FOR’]. These variables will provide the exact IP address of the client whether he/she is using a proxy server or not.

Example : An example of getting an Internet Protocol address using $_SERVER[‘HTTP_CLIENT_IP’] and $_SERVER[‘HTTP_X_FORWARDED_FOR’] superglobal variables is shown below.

php-getipoutput2-1024x525

Output : You will get the following output of the above php code.

IMPORTANT FACTS ABOUT IP ADDRESSES

  1. Till now, two IP versions has been created namely IPv4(Internet Protocol Version 4) and IPv6(Internet Protocol Version 6).
  2. The size of an IP address in IPv4 is 32-bit.
  3. The size of an IP address in IPv6 is 128-bit.
  4. The space of the IP address is controlled by the American agency IANA(Internet Assigned Numbers Authority) which is founded in 1988.
  5. Both the IP versions have different notations. For Example, IPv4(192.14.267) and IPv6(2004:db8:a0b:1357:0:436:8:2)

This article has been written in the simplest way in order to give you the best possible information about how to get the IP address of a client or a visitor using php programming language. Hope you liked this article. For further updates, stay tuned to our php blogs

Facebook
Twitter
LinkedIn
Pinterest