Question

I'm doing an assignment where I'm supposed to show some text with the MIME-type "text/plain".

<!DOCTYPE html>
<head lang="sv">
    <meta charset=utf-8" />
    <title>A Title</title>
</head>
<body>
    <?php
    header("Content-Type: text/plain");
    echo("Hello");
    ?>
</body>
</html>

And "hello" is shown when I load the page. But the problem is that everything else except the PHP code is also shown. That means that the result is a page where all of the HTML code gets print out, as well as the text that I've printed using the echo method.

I don't really know why I'm supposed to show the text using the MIME-type "text/plain", but the assignment says so. Also I'm pretty new to PHP, so this problem might be very easy. But at least I couldn't find any other people who have had the same problem.

So what I want is to show only the text that's in my echo method, and it should have the MIME type "text/plain". Is that even possible?

Était-ce utile?

La solution

Setting header content-type to text/plain will tell the browser that the file is a text file, hence the HTML markup will be shown as text too.
I would guess that the html is not supposed to be in the file at all, so just remove it and it will look like you want it to.

Other than that, headers are supposed to be set before any type of output, so set them at the beginning of the file, before any output.

<?php
  header("Content-Type: text/plain");
  echo("Hello");

Autres conseils

Actually you code is okay.If you run it, you will see that it converts the whole document into plain text...you can change the content-type to text/html and see it giving you hello only. Let me know if I answer your question...

You must send the header before any HTML code

For this reason, your header is not sent (because some HTML code is already print like your doctype) and as no effect

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top