How to Display the Visitor’s IP Address on a Website Using PHP?


An IP address (Internet Protocol Address) is a unique numerical identifier assigned to a device on a network. Just like every house on a street has its own address, every computer, smartphone, or tablet connected to the internet has a unique IP address.

Many websites use IP addresses to track user activity or customize content based on location. You’ve probably seen something like this on websites:

Your IP address: 192.168.0.1

Let’s create our own IP address display using PHP!

PHP Code Example

To display the IP address on your website, use the following simple code:

<?php
echo 'Your IP address: ' . $_SERVER['REMOTE_ADDR'];
?>

How to Embed PHP Code in HTML?





Here’s an example of a complete HTML page that displays the IP address:

<!DOCTYPE html>
<html>
<head>
    <title>IP Address Display - Your Website</title>
</head>
<body>
    <h3>Your IP address:</h3>
    <p><?php echo $_SERVER['REMOTE_ADDR']; ?></p>
</body>
</html>

Now, when the page is opened, the visitor will see their IP address! Use this method to make your website more interactive and personalized.