How to create your own What Is My IP service

Utilizing What Is My IP services is great, you perform a query to a server which is not yours, and they will tell you which external IP address you are using.

This can be done in a better way, we need to have our own What Is My IP service which is lightweight and does exactly what we want.

In this how to create your own What Is My IP service tutorial we are going to do exactly that.

Python, Webhosting and PHP

For this tutorial we are going to use Python, and we expect that we already have our own web hosting environment up and running. This is needed as we need our own online server which is going to provide our external IP address back.


The Python code which you see below will get the page which has been set as ‘URL’, and it will then filter for the H1 header and the end of the H1 header, as in between the header HTML code, we have stored the external IP address.

import requests
url = ''
r = requests.get(url)
ip = r.text.split('<h1>')[1].split('</h1>')[0]


On your webhosting environment, you will need to place a PHP file which contains some code,this code will return a page as shown in the picture below.

For example, with Apache, you will need to store the content in the following directory:

We have changed the IP, but you will get the full IP in your screen 🙂

The PHP code

Create a PHP file on your webserver, and add the code seen below into the file. Store the file as ‘getip.php’.

<title> Self IP Check</title>
                <div id="ip-lookup" class="tools">
                        <?php if ($_SERVER["HTTP_X_FORWARDED_FOR"] != "") {
                                $IP = $_SERVER["HTTP_X_FORWARDED_FOR"];
                        } else {
                                $IP = $_SERVER["REMOTE_ADDR"];
                        } ?>
                        <h1><?php echo $IP; ?></h1>
<h2><a href="">Head back to</a></h2>

Share This Message