这篇文章主要介绍了PHP中的output_buffering详细介绍,本文讲解了output buffering的一些高级用法,需要的朋友可以参考下
我个人认为,Output buffering是比较纯粹的4.0特征。尽管从概念上看来相当简单,但是output buffering功能非常强大,能使开发者更容易地开发高级而有效的程序。
本文将介绍HTTP header,以及output buffering如何帮助您处理HTTP header,并介绍了output buffering的一些高级用法。
HTTP Header
对 于使用HTTP 协议建立的每个请求,Web服务器产生的响应通常包括两个部分 – 标题和主体。例如,如果在Web服务器的文档根目录下有一个小文本文件,叫做example.txt,文件中包含文本Hello, world!,那么对此文件的HTTP 请求响应如下所示:
复制代码 代码如下:
HTTP/1.1 200 OK
Date: Sat, 02 Sep 2000 21:40:08 GMT
Server: Apache/1.3.11 (Unix) mod_macro/1.1.1 PHP/4.0.2-dev
Last-Modified: Sat, 02 Sep 2000 21:39:49 GMT
ETag: "12600b-e-39b173a5"
Accept-Ranges: bytes
Content-Length: 14
Connection: close
Content-Type: text/plain
Hello, world!
这 个请求中的第一部分(就是较多的那部分)就是HTTP header。虽然用户在浏览器中看不到HTTP header,但它包含了用于浏览器的信息,例如文档内容类型,使用的协议版本,文档的最后更改日期等等。HTTP header并没有太多的规则,通常情况下,它的格式如下:
复制代码 代码如下:
Field: Value[字段:值]
必须用空行将它们和文档主体分开。
可以从PHP脚本添加或更改此HTTP header的信息。例如,可以使用 header() 函数:
复制代码 代码如下:
header("Location: "); // 重定向到
也可以使用 SetCookie() 函数:
复制代码 代码如下:
SetCookie("foo", "bar");
你可能会知道HTTP cookies是使用 HTTP headers 来实现的。例如,以下PHP文件的 HTTP 请求响应
复制代码 代码如下:
SetCookie("foo", "bar");
print "Set cookie.";
?>
将会是这样的:
复制代码 代码如下:
HTTP/1.1 200 OK
Date: Sat, 02 Sep 2000 21:43:02 GMT
Server: Apache/1.3.11 (Unix) mod_macro/1.1.1PHP/4.0.2-dev
X-Powered-By: PHP/4.0.2-dev
Set-Cookie: foo=bar
Connection: close
Content-Type: text/html
Set cookie.
浏览器读取从服务器返回的 HTTP header,知道送来了一个叫做 foo 的 cookie (在这里是一个 session cookie),它的值是 bar。
为什么要使用Output Buffering技术
早 在PHP/FI 2.0时就很明显需要output buffering技术了。如果你使用过这种版本的PHP,那么可能还记得经常会碰到 Oops, SetCookie called after header has been sent 这个错误消息,并使你捎头抓耳,也弄不清是什么原因。
如 果你已使用过PHP的最新版本 -- PHP 3.0 甚至 PHP 4.0 -- 那么你会知道这个错误消息: Oops, php_set_cookie called after header has been sent。或者,你在试图调用 PHP 的 header() 函数时会遇到 Cannot add header information - headers already sent 消息。一般来说,output buffering技术用户避免这些烦人的错误消息,同时开发人员也可用于高级的用途。
这些错误是什么时候产生的呢?如果你在已经发送了HTTP header之后试图添加或修改标题信息,以及在文档主体和标题之间缺少空行时,,就会产生这些错误消息。为了理解这是如何产生的,让我们来看看PHP是如何处理HTTP header输出和主体输出的。
脚本开始执行时,它可以同时发送header(标题)信息和主体信息。
Header信息(来自 header() 或 SetCookie() 函数)并不会立即发送,相反,它被保存到一个列表中。
这样就可以允许你修改标题信息,包括缺省的标题(例如 Content-Type 标题)。但是,一旦脚本发送了任何非标题的输出(例如,使用块或 print() 调用),那么PHP就必须先发送所有的标题,然后再送出空行,终止 HTTP header,而在此之后才会继续发送主体数据。从这时开始,任何添加或修改标题信息的试图都是不允许的,并会发送上述的错误消息之一。
虽然这并不会引起多大的问题,有时候只是在发出任何输入之前终止HTTP header,从而引起脚本逻辑的复杂化而已。Output buffering技术可以解决这些问题。
Output Buffering的工作原理
启用output buffering时,在脚本发送输出时,PHP并 不发送HTTP header。相反,它将此输出通过管道(pipe)输入到动态增加的缓存中(只能在PHP 4.0中使用,它具有中央化的输出机制)。你仍然可以修改,添加标题行,或者设置cookie,因为标题实际上并没有发送。最简单的情况是,当脚本终止 时,PHP将自动发送HTTP header到浏览器,然后再发送输出缓冲中的内容。这简单吧。
基本用法
可以使用下面的四个函数,它们可以帮助你控制output buffering:
复制代码 代码如下:
ob_start()
启用output buffering机制。
Output buffering支持多层次 -- 例如,可以多次调用 ob_start() 函数。
ob_end_flush()
发送output buffer(输出缓冲)并禁用output buffering机制。
ob_end_clean()
清除output buffer但不发送,并禁用output buffering。
ob_get_contents()
将当前的output buffer返回成一个字符串。允许你处理脚本发出的任何输出。
此外,可以启用 php.ini 中的 output_buffering 指令。如果启用了此指令,那么每个PHP脚本都相当于一开始就调用了ob_start()函数。
Example 1
复制代码 代码如下:
Example 1
print "Hello, $user ";
SetCookie("Wow", "This cookie has been set even though we've already emitted output!");
?>
这里,尽管你已发送了输出(HTML代 码块中和 print 语句中),也可以使用 SetCookie() 调用,而不会出错,真的要感谢output buffering机制。请注意使用output buffering机制用于这种目的会引起一定程度上的性能损失,因此最好缺省情况下不要启用此机制。但是,对于复杂一些的脚本,output buffering可以简化逻辑性。
Example 2
复制代码 代码如下:

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
