Rendering plain text through PHP

久未见 提交于 2019-12-22 04:03:48

问题


For some reason, I want to serve my robots.txt via a PHP script. I have setup apache so that the robots.txt file request (infact all file requests) come to a single PHP script.

The code I am using to render robots.txt is:

echo "User-agent: wget\n";
echo "Disallow: /\n";

However, it is not processing the newlines. How to server robots.txt correctly, so search engines (or any client) see it properly? Do I have to send some special headers for txt files?

EDIT 1:

Now I have the following code:

header("Content-Type: text/plain");
echo "User-agent: wget\n";
echo "Disallow: /\n";

which still does not display newlines (see http://sarcastic-quotes.com/robots.txt ).

EDIT 2:

Some people mentioned its just fine and not displayed in browser. Was just curious how does this one display correctly: http://en.wikipedia.org/robots.txt

EDIT 3:

I downloaded both mine and wikipedia's through wget, and see this:

$ file en.wikipedia.org/robots.txt
en.wikipedia.org/robots.txt: UTF-8 Unicode English text

$ file sarcastic-quotes.com/robots.txt
sarcastic-quotes.com/robots.txt: ASCII text

FINAL SUMMARY:

Main issue was I was not setting the header. However, there is another internal bug, which is making the Content-Type as html. (this is because my request is actually served through an internal proxy but thats another issue).

Some comments that browsers don't display newline were only half-correct -> modern browsers correctly display newline if content-type is text/plain. I am selecting the answer that closely matched the real problem and was void of the above slightly misleading misconception :). Thanks everyone for the help and your time!

thanks

JP


回答1:


Yes, you forgot to set the Content Type of your output to text/plain:

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

Your output is probably being sent as HTML, where a newline is truncated into a space, and to actually display a newline, you would need the <br /> tag.




回答2:


  1. header('Content-Type: text/plain') is correct.
  2. You must call this method before anything is written to your output, including white space. Check for whitespace before your opening <?php.
  3. If your Content-Type header has been set to text/plain, no browser in its right mind would collapse whitespace. That behaviour is exclusive to HTML and similar formats.
  4. I'm sure you have your reasons, but as a rule, serving static content through PHP uses unnecessary server resources. Every hit to PHP is typically a new process spawn and a few megs of memory. You can use apache config directives to point to different robots files based on headers like User-Agent - I'd be looking into that.
  5. It's likely that search engines ignore the Content-Type header, so this shouldn't be an issue anyway.

Hope this helps.

-n




回答3:


<?php header("Content-Type: text/plain"); ?>
User-agent: wget
Disallow: /

BTW, the newlines are there just fine. They're just not displayed in a browser. Browsers collapse all whitespace, including newlines, to a single space.

deceze$ curl http://sarcastic-quotes.com/robots.txt
User-agent: wget
Disallow: /



回答4:


You must set the content type of the document you are serving. In the case of a .txt text file:

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

The IANA has information about some of the more popular MIME (content) types.




回答5:


i was having a similar issue and either "\n" nor PHP_EOL worked. I finally used:

header('Content-Disposition: attachment; filename="plaintext.txt"');
header("Content-Type: text/plain");
echo "some data";
echo chr(13).chr(10);

The echo of BOTH characters did the trick. Hope it helps someone.

Bye anankin




回答6:


If you are using echo, then use <br> for new lines. the printf function is what uses \n.

In your case, use printf because you are not using HTML. I believe this is the proper way to do this, along with setting the MIME type to text.



来源:https://stackoverflow.com/questions/4506679/rendering-plain-text-through-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!