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.

Python

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 = 'https://cyberwarzone.com/getip.php'
r = requests.get(url)
r.text
ip = r.text.split('<h1>')[1].split('</h1>')[0]
print(ip)

Webhosting

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:

/var/www/html/getip.php
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’.

<html>
<center>
<title>Cyberwarzone.com 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="https://www.cyberwarzone.com">Head back to Cyberwarzone.com</a></h2>
</center>
</html>

Share This Message