October 2, 2022

PHP: Print out your users IP Adress

It is fairly easy to find out your clients IP address when using PHP. The PHP script is basically one single line and returns the IP address that can be accessed for example as String.

See the PHP code example below to find out how to receive the clients IP address.

PHP Ip Address REMOTE_ADDR
<?php
    echo "<b>Your IP address:</b> ".$_SERVER["REMOTE_ADDR"];
?>

The code stated above creates the following output:

Your IP address: 3.223.3.251

The IP Adress may be used as a String. Regular expressions are a common way to deal with IP addresses in String representation.

If you are presenting you website in different languages you may use the IP address to find out the rough location of a visitor to present your contents accordingly.

To find out how the IP adresses are distributed geographically around the world take a look at the IANA: http://www.iana.org/

This article is also available in German:

Other articles that might be of your interest:

Leave a Reply

Your email address will not be published.