Get location from IP address - PHP project source code

Have you ever tried to create an IP address tracker in PHP? There are several ways to fetch the location details of an IP address. This is one of and easiest ways to do that. 

So in this post, I am going to share with you an awesome and simple PHP tool with source code that helps you to display the location details of any IP address. You can use this tool to check any website's server location, the IP address location of a particular device, and your IP address location. 

In this tutorial, we are using a web URL that enables us to fetch the location from an IP address. 

loading

We have already created a post showing another way to get the IP location. So keep in touch with fluratech.

You must remember

  1. It is not an illegal process.
  2. You can use this script in your project
  3. You can modify it as you like.
  4. There are not any restrictions for using this script in live web tools. 

Files and folder

Create 3 files named index.php, process.php, and style.css in a folder. 

How to use this tool?

Copy and paste all the code given below also you can modify it. Make sure your XAMPP server is running well and those files are created in htdocs folder(only for the XAMPP server). 

After running the code you can see an awesome-looking IP address locator tool. You can search URL or IP address in the search bar. If you fill it blank or the ip address or url entered is not working then it would show your IP location.  

The url must contain HTTP or HTTPS protocol.

index.php


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>IP locator</title>

    <!-- linking CSS  -->
    <link rel="stylesheet" href="style.css">

</head>

<body>
    <center style="color:white; padding-top:20px; font-weight:900; font-size:large;">
        <h1>IP locator </h1>
    </center>
    <div class="mainwrapper">
        <form action="#" method="get">
            <input type="text" name="weburl" id="weburl" placeholder="IP / https://fluratech.com">
            <input type="submit" name="submit" value="Submit">
        </form>


        <div class="output">
             <?php
                $weborip = $_GET["weburl"];
                if (isset($_GET["submit"])) {
                    if (filter_var($weborip , FILTER_VALIDATE_URL)) {
                        $weborip = preg_replace("(^https?://)", "", $weborip);
                        $ip = gethostbyname($weborip);
                        include 'process.php';
                    } else {
                        $ip = $weborip;
                        include 'process.php';
                    }
                    
                }
                ?>
        </div>
    </div>
</body>

</html>

style.css


* {
    margin: 0%;
    padding: 0%;
    box-sizing: border-box;
}

body {
    background-image: linear-gradient(to left, red, rgb(217, 255, 0));
    width: 100%;
    height: 100%;
}

.mainwrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 10px;
    padding: 10px;
}

.mainwrapper input {
    outline: none;
    padding: 5px;
    font-size: 18px;
    font-weight: 600;
}

.output {
    width: 500px;
    text-align: center;
    font-weight: 800;
    background-color: white;
    border-radius: 20px;
    padding: 10px;
    margin: 20px;
}

process.php


<?php

// $ip = '52.25.109.230';
// $ip = gethostbyname("godaddy.com");  

$ipdat = @json_decode(file_get_contents(
	"http://www.geoplugin.net/json.gp?ip=" . $ip));
echo  "IP ADDRESS     : $ipdat->geoplugin_request <br>";
echo  "COUBTRY NAME   : $ipdat->geoplugin_countryName <br>";
echo  "COUNTRY CODE   : $ipdat->geoplugin_countryCode <br>";
echo  "TIME ZONE      : $ipdat->geoplugin_region <br>";
echo  "CITY           : $ipdat->geoplugin_city <br>";
echo  "CONTINENT      : $ipdat->geoplugin_continentName <br>";
echo  "CURRENCY       : $ipdat->geoplugin_currencySymbol <br>";
echo  "CURRENCY CODE  : $ipdat->geoplugin_currencyCode <br>";
?>

Conclusion 

Congratulations we have successfully created an awesome-looking IP address location checker tool. I hope that this post is useful for your web development career. Write your experience and doubts related to this post. Also, don't forget to comment on your valuable suggestions and opinions. 

Discussions

Post a Comment