美文网首页PHP程序员
网站开发者的『输出缓冲』入门指导

网站开发者的『输出缓冲』入门指导

作者: Kaiyulee | 来源:发表于2017-03-17 10:53 被阅读25次

如果你还没有使用 PHP 的output buffering,那么现在开始吧!

这是一篇专门写给web developer的文章,我会在几秒钟内举几个小李子让大家来明白什么是output buffering

简单来说,不使用 output buffering,你的网页(即 HTML)是在PHP执行时一段一段渲染到浏览器的;使用 output buffering 的话,你的
HTML 会被当做一个整体储存在一个变量中,然后一下子在浏览器中渲染出来,你是不是已经在思考性能上的优势以及网页传递时给你提供了某些可能?

优势

  • 开启 output buffering 会减少HTML的渲染次数(显然滴)
  • 所有用来处理字符串的函数,可以拿来处理整个网页了,毕竟它是一个变量了
  • 如果曾经遇到过 Warning: Cannot modify header information - headers already sent by (output) 的问题,你会很开心 output buffering 就是答案

Hello World

<?php
# start output buffering at the top of our script with this simple command
ob_start();
?>
<html>
    <body>
        <p>Hello World</p>
    </body>
</html>
<?php
# end output buffering and send our HTML to the browser as a whole
ob_end_flush();
?>

就是这么简单!

进一步,压缩输出

下面,将 ob_start() 改成 ob_start('ob_gzhandler'),这个小改动将会压缩我们的HTML,是页面的加载体积变小。

<?php
# start output buffering at the top of our script with this simple command
# we've added "ob_gzhandler" as a parameter of ob_start
ob_start('ob_gzhandler');
?>
<html>
<body>
    <p>Hello World!</p>
</body>
</html>
<?php
# end output buffering and send our html to the browser as a whole
ob_end_flush();
?>

更进一步,更改传输内容

下面,ob_start() 变成了 ob_start('ob_postprocess')ob_postprocess() 是我们自定义的一个方法,用来在输出到浏览器之前更改HTML内容,Hello World 将会改成 Aloha World

<?php
// start output buffering at the top of our script with this simple command
// we've added "ob_postprocess" (our custom post processing function) as a parameter of ob_start
ob_start('ob_postprocess');
?>

<html>
<body>
<p>Hello world!</p>
</body>
</html>

<?php
// end output buffering and send our HTML to the browser as a whole
ob_end_flush();

// ob_postprocess is our custom post processing function
function ob_postprocess($buffer)
{
    // do a fun quick change to our HTML before it is sent to the browser
    $buffer = str_replace('Hello', 'Aloha', $buffer);
    // "return $buffer;" will send what is in $buffer to the browser, which includes our changes
    return $buffer;
}
?>

提示,在 output buffering 开启的情况下,我们可以在任何时候设置 cookies 了。

因为HTML并不直接发送到浏览器了,我们可以毫无顾忌的在我们的PHP脚本中设置COOKIE。赶紧 turn it on 吧!

更多其妙用法

相关文章

  • 关于『输出控制』的其妙用法

    如果你读了上篇文章网站开发者的『输出缓冲』入门指导,你会发现 PHP output buffering 是很有用的...

  • 网站开发者的『输出缓冲』入门指导

    如果你还没有使用 PHP 的output buffering,那么现在开始吧! 这是一篇专门写给web devel...

  • 2020-07-03字节缓冲流

    字节缓冲流 字节缓冲流类介绍①BufferedOutputStream:该类实现缓冲输出流。通过设置这样的输出流,...

  • php output control

    php 的输出控制方法主要包括以下: flush— 刷新输出缓冲 ob_clean— 清空(擦掉)输出缓冲区 ob...

  • linux 标准io笔记

    三种缓冲 1.全缓冲:在缓冲区写满时输出到指定的输出端. 比如对磁盘上的文件进行读写通常是全缓冲的.2.行缓冲:在...

  • ionic2优秀资源整理

    网上关于ionic的资源比较少, 好多刚入门的开发者找不到入门指导资源, 下面就推荐一些个人觉得比较好的资源; 一...

  • channel详解

    无缓冲通道 只有当读写都准备好时才不会被阻塞 输出: 有缓冲通道 当有缓冲通道长度未满时,写入是无阻塞的 输出: ...

  • PHP的输出缓冲区

    PHP的输出缓冲区 什么是缓冲区?简单而言,缓冲区的作用就是,把输入或者输出的内容先放进内存,而不显示或者读取.至...

  • FFmpeg - 音频重采样

    音频重采样步骤 创建采样上下文 设置输入缓冲区 设置输出缓冲区 打开文件开始重采样 检查输出缓冲区是否还有残余的样...

  • 字节流和字符流的区别&常用方法总结

    参考:深入理解Java中的IO · 节流没有缓冲区,是直接输出的,而字符流是输出到缓冲区的。因此在输出时,字...

网友评论

    本文标题:网站开发者的『输出缓冲』入门指导

    本文链接:https://www.haomeiwen.com/subject/hgqenttx.html