≡ Menu

How to find out the client’s IP address in PHP?

The question is – how to find out the remote client’s IP address in PHP?

There is a very simple method to do this. Just use the $_SERVER. The $_SERVER variable has several fields that are indispensable for the PHP programmer. The remote IP address is just one of them. To see the whole list of $_SERVER variables, use this page.

To find the remote IP address, use the following PHP code snippet:

$ipaddress = $_SERVER['REMOTE_ADDR'];

The REMOTE_ADDR string is indexed in the $_SERVER array which is populated by PHP before the PHP script is run. This is the easiest and the most straightforward way to find the client’s calling IP address.

You can also find out the remote TCP port that the client is using. This can be done using the string ‘REMOTE_PORT” passed to the $_SERVER array.

$remote_port = $_SERVER['REMOTE_PORT'];

These statements work on any system be it Windows or any flavor of Linux like Debian, Fedora, CentOS etc. Remember that the remote address is always populated no matter where the request is coming from and what the client is requesting.


Comments on this entry are closed.