Home > Net >  How to open an URL via browser and output a simple text file rather than HTML code?
How to open an URL via browser and output a simple text file rather than HTML code?

Time:11-01

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!

CodePudding user response:

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;
?>

CodePudding user response:

Thanks @Mahen, I did some changes here and there and it looks much better now. the issue is now properly waiting for the getJSON function to actually terminate. I am playing with the suggestions at this link: https://splunktool.com/function-wait-with-return-until-getjson-is-finished but no luck so far. What I did was to add a setTimeout to function ambil() and this way there is some waiting and most of the times the code moves ahead when getJSON returns some values

You can try it here: https://alterego.cc/mypublicip/firstPage.php

firstPage.php

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mypublicipstyle.css">
<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" hidden></p><br>
<button type="button" id="submit" onClick="ambil()" style="display: none;"></button>
<form id="lanjut" action="secondPage.php" method="get" style="display:none">
<input id="tes" name="tes" type="text" value="">
</form>
<script>

setTimeout(function ambil(){
    document.getElementById('tes').value = document.getElementById('mpi').innerHTML;
    document.getElementById('lanjut').submit();
    }, 5000);
</script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#submit").click();
    });
</script>
</body>
</html>

secondPage.php

<?php
if(isset($_GET['tes'])){
$getIP = $_GET['tes'];
}
echo $getIP;
?>

mypublicipstyle.css

button {
    visibility: hidden;
}

CodePudding user response:

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.

  • Related