skip to Main Content

I have built this small HTML file you can see here https://alterego.cc/mypublicip/ that returns your public IP. If you inspect the code of the page you can actually see the HTML in there, because that’s an HTML file of course

What I would actually like to achieve is something like this https://wtfismyip.com/text (from someone else) where if you inspect the code you can see it’s just a text file there. No additional tags or anything in particular

How could I achieve the same result?

I have tried a bit of everything but I always end up having some HTML code in there. In particular with DIV and innerText but no particular luck so far. I believe I am following the wrong approach and there is something I am missing

Thanks!

2

Answers


  1. I create 2 files. firstPage.php and secondPage.php

    Try this code.

    firstPage.php

    <!DOCTYPE html>
    <html>
    <head>
    <title>What is my Public IP address?</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
    </script>
    <script>
    $.getJSON("https://api.ipify.org?format=json", function(data) {
        $("#mpi").html(data.ip);
        $("#tes").html(data.ip);
        document.getElementById('tes').value = html(data.ip);
    })
        mpi = mpi.replace(/s|n|&nbsp;/g, ' ');
        mpi = mpi.replace(/<[^>]+>/gm, '');
    
    </script>
    </head>
    <body>
    <p id="mpi"></p><br>
    <button type="button" onClick="ambil()">Give me this IP in plain text</button>
    <form id="lanjut" action="secondPage.php" method="get" style="display:none">
    <input id="tes" name="tes" type="text" value="">
    </form>
    <script>
    function ambil(){
        document.getElementById('tes').value = document.getElementById('mpi').innerHTML;
        document.getElementById('lanjut').submit();
        }
    </script>
    </body>
    </html>
    

    secondPage.php

    <?php
    if(isset($_GET['tes'])){
    $getIP = $_GET['tes'];
    }
    echo $getIP;
    ?>
    
    Login or Signup to reply.
  2. What you need is to set the content type of the response to plain text instead of html:

    header("Content-Type: text/plain");
    

    Place this in the first line of your php code of the will do.

    However, you won’t be able to set head tags in HTML, which means you will have to remove the title and script tag, etc. as they will be displayed as plain text and won’t be rendered.

    EDIT: Your content is displayed by grabbing data through ajax, but you can simply just use one file of php and echo the ip out.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search