简体   繁体   中英

HTTP headers in php, perl and python

In both perl and python, you have to print some headers (if you want) and a new line before the page content. Otherwise you get an error - End of script output before headers .

In php things are different. Whenever you print something (using either print "" or echo "" ), it is assumed as a plain text, even if you try to print a header. To do this (print a header), you should use the header() function (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 place a new line before text in perl in python but not in php. The same code in similar languages has such a different result?

Why in perl and python you print() the headers but in php you header() them?

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

PHP follows the "X Server Pages" processing model - ASP and JSP are two other examples of this paradigm - everything in the template is text to be written to the client unless wrapped in special tags which cause the PHP engine to execute code. Think of those chunks of text as being implicitly wrapped print statements. In general, the output is buffered until processing reached the end of the template, at which point the accumulated headers are written followed by the accumulated page output buffer.

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

So your Perl program is the lowest level processing you can get. You're doing all of it from scratch because you're using a general purpose programming language and not using any modules that package higher-level web processing functionality.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM