PHP-Get public IP address instead of localhost IP address

To get the IP address of user we can use the below code snippet.

If you are running the above code in local host you will get the localhost IP address as shown below.

With the below code and using an external website we can get the current public ip into a variable as shown below.

The output for the above code run on server is shown below.