HTTP headers in php, perl and python

In both perl and python, you need to print some headers (if you like) and a newline before the page content. Otherwise, you will receive an error message - . End of script output before headers

Things are different in php. Whenever you type something (using print ""

or echo ""

), it is assumed to be plain text, even if you are trying to print a title. To do this (print the title) you must use a function header()

(which is not available in perl and python).
Some examples:

<?php
# this is a plain text in the page, you can use 'echo ""' to get the same result
print "Content-type: text/plain; charset=utf-8\n\n";

      


#!usr/bin/perl 
# this is a header with two new lines after it (without them you get the error)
print "Content-type: text/plain; charset=utf-8\n\n";
# (the same is in python)

      


<?php
print "Some text, no headers are sent, no new lines are printed and you get no errors";

      


#!usr/bin/perl
print "Some text, no headers are sent, no new lines are printed and you get an error.";
# (the same is in python)

      

You have to put a newline before text in perl in python, but not in php. Does the same code in similar languages ​​have the same result?

Why in perl and python you have print()

headers, but in php you have header()

them?

Why don't you need to print a new line in php to say "end of headers"?

+3


source to share


1 answer


PHP follows the "X Server Pages" processing model - ASP and JSP are two more examples of this paradigm: everything in the template is text that needs to be written to the client, unless it has been wrapped with special tags that force the PHP module to execute code Consider these snippets of text are implicitly wrapped printed statements. In general, output is buffered until processing completes before the end of the template, after which the accumulated headers are written, followed by the accumulated page output buffer.

The Perl code you provided is an example of CGI programming and using raw Perl or using a CGI module, everything in the file (plain old Perl program) is code and the output is sent to the client in the order it is written. Since the HTTP protocol says that you send the headers first and then you send the page content, why should you have print

your own headers before print

your page content.

So, your Perl program is the lowest degree of processing you can get. You are doing all of this from scratch because you are using a general-purpose programming language and not using any modules that install higher-level web processing functionality.



PHP, on the other hand, is a higher-level software system specifically for generating HTML pages. Therefore PHP knows that you want headers as well as content (while Perl just thinks you are printing stuff) and expects you to use the header () function it provides.

I suspect that you are doing things in Python in a similar way to what you are doing in Perl.

+3


source







All Articles