Providing plain text through PHP

For some reason, I want to serve my robots.txt through a PHP script. I have apache setup, so the robots.txt file request (infact all file requests) comes to one PHP script.

The code I use to render robots.txt is:

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

However, it does not process newlines. How to download a robots.txt file correctly so that search engines (or any client) see it correctly? Should I send 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 newline characters (see http://sarcastic-quotes.com/robots.txt ).

EDIT 2:

Some people have noted that this is just fine and does not appear in the browser. It was just curious how it looks right: http://en.wikipedia.org/robots.txt

EDIT 3:

I downloaded both mine and Wikipedia via wget, and I see the following:

 $ 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:

The main problem: I did not configure the header. However, there is another internal error that creates a Content-Type as html. (this is because my request is actually served through an internal proxy, but this is another problem).

Some comments that browsers do not display a new line were only half correct. → modern browsers correctly display a new line if the content type is text / simple. I choose an answer that exactly matches the real problem and was devoid of the above slightly misleading misconception :). Thank you all for your help and your time!

thanks

In JP

+8
php text header robots.txt plaintext
source share
6 answers

Yes, you forgot to set the content type of your output to text/plain :

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

Your output is probably sent as HTML, where the new line is truncated into space and actually displays the new line, you need a <br /> tag.

+19
source share
  • header('Content-Type: text/plain') is correct.
  • You should call this method before , everything that is written to your output, including a space. Check the spaces before opening. <?php .
  • If your Content-Type header was set to text/plain , no browser in your mind would break the spaces. This behavior is exclusive to HTML and similar formats.
  • I am sure that you have your own reasons, but, as a rule, using static content through PHP uses unnecessary server resources. Each hit in PHP is usually a new process and a few megabytes of memory. You can use apache configuration directives to point to different robot files based on headers such as User-Agent - I would consider this.
  • Search engines probably ignore the Content-Type header, so this should not be a problem anyway.

Hope this helps.

-n

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

By the way, the new lines there are just wonderful. They simply do not appear in the browser . Browsers fold all spaces, including newlines, into one space.

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

You must specify the type of content of the document that you are serving. In the text file .txt:

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

IANA has information on some of the most popular types of MIME (content) .

0
source share

i had a similar problem and worked either "\ n" or PHP_EOL. 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 the BOTH characters did the trick. Hope this helps someone.

Bye anankin

0
source share

If you use echo, use <br> for newlines. the printf function uses \ n.

In your case, use printf because you are not using HTML. I believe this is the right way to do this, and also set the MIME type for the text.

-one
source share

Source: https://habr.com/ru/post/650371/


All Articles