1

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
Jonatan Stenbacka
  • 1,824
  • 2
  • 23
  • 48
  • You should have gotten a *big fat warning* from PHP when running this code. Please make sure that your development environment has `error_reporting` cranked all the way up! See also, the dupe containing the actual error message you aren't seeing. – Charles May 19 '14 at 18:45

3 Answers3

5

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");
Jite
  • 5,761
  • 2
  • 23
  • 37
1

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...

wanjiku
  • 251
  • 6
  • 14
0

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

Gwenc37
  • 2,064
  • 7
  • 18
  • 22
  • not true. header is set by php prior to any html being rendered.yes there are edge cases where calling header inside of html destroys the script, but this isnt' one of them. if it was, he would get a "headers already sent" notice from php. – r3wt Apr 10 '14 at 11:28
  • 2
    @r3wt Only if php is set to show notices (which it should be in a development environment, but I would guess its not in this case, cause the notice SHOULD be there..). – Jite Apr 10 '14 at 11:32
  • @Jite according to the op it printed out the html as plaintext. the reason is simple. php only sends the headers after a closing php tag or header() function(which ever occurs first). – r3wt Apr 10 '14 at 11:34
  • 1
    @r3wt which it will, without a warning if warnings are off. Test it! :) (i get this when i run the code: `Warning: Cannot modify header information - headers already sent`) – Jite Apr 10 '14 at 11:37
  • @Jite: i'm on php 5.5 locally and hhvm on my test server. both printed the document as plain text, not an entry in the logs in site. – r3wt Apr 10 '14 at 11:40
  • Set `error_reporting = E_ALL`, should show in ALL php versions. – Jite Apr 10 '14 at 11:41
  • Its also possible that you have output_buffering on. – Jite Apr 10 '14 at 11:44
  • not a peep with `` – r3wt Apr 10 '14 at 11:48
  • @r3wt I get the warning above on php v - 5.5.9, 5.4.24, 5.4.4, 5.2.17, 5.3.20. Your php settings are probably not as they should (for a development machine) if you do not. – Jite Apr 10 '14 at 11:56
  • @Jite: html doesn't even get sent until the first closing php tag OR header call. i already told you that. therefore, the header would be sent first in this case, even though its inside of the body tag. – r3wt Apr 10 '14 at 20:13
  • @r3wt Now, calling me an idiot don't make you right. Its not my fault your php is configured wrong. The warning i showed you before is produced by the code in the question on every versions i wrote in my previous comment. You can tell me whatever you wish to tell me, that don't magically make you right tho. – Jite Apr 10 '14 at 20:33