简体   繁体   English

php,perl和python中的HTTP标头

[英]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. 在perl和python中,您必须在页面内容之前打印一些标题(如果需要)和新行。 Otherwise you get an error - End of script output before headers . 否则会出现错误 - End of script output before headers

In php things are different. 在PHP中,事情是不同的。 Whenever you print something (using either print "" or echo "" ), it is assumed as a plain text, even if you try to print a header. 无论何时打印某些内容(使用print ""echo "" ),即使您尝试打印标题,也会将其视为纯文本。 To do this (print a header), you should use the header() function (which is not available in perl and python). 要执行此操作(打印标题),您应该使用header()函数(在perl和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. 你必须在python中的perl文本之前放置一个新行,而不是在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? 为什么在perl和python中你print()标题但在php中你header()它们?

Why in php you don't have to print new line to say "end of headers"? 为什么在php中你不必打印新行来说“标题结束”?

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. PHP遵循“X Server Pages”处理模型 - ASP和JSP是此范例的另外两个示例 - 模板中的所有内容都是要写入客户端的文本,除非包含在导致PHP引擎执行代码的特殊标记中。 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. 您提供的Perl代码是CGI编程的示例,使用原始Perl或使用CGI模块,文件中的所有内容(一个普通的旧Perl程序)都是代码,输出发送到客户端命令它写的。 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. 由于HTTP协议说,首先你发送标题,然后发送页面内容,这就是为什么你要print你的头,你之前print您的网页内容。

So your Perl program is the lowest level processing you can get. 因此,您的Perl程序是您可以获得的最低级别处理。 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. 您正在从头开始做所有这些,因为您使用的是通用编程语言,而不是使用任何包含更高级别Web处理功能的模块。

PHP, on the other hand, is a higher-level program system, specifically for generating HTML pages. 另一方面,PHP是一个更高级别的程序系统,专门用于生成HTML页面。 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. 所以PHP知道你需要标题和内容(而Perl只是认为你正在打印东西),并期望你使用它提供的header()函数。

I suspect that you're doing things in Python in a similar way to what you're doing in Perl. 我怀疑你在Python中做的事情与你在Perl中做的事情类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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