我的本地环境 windows + apche + php5.2

今天,碰到一个诡异的问题,以前认为  php 脚本中调用 heade()函数之前不能有任何的如 echo,print ,print_r,var_dump等输出,否则的话就会报错。

但是,

<?php
header( 'Expires: Mon, 26 Jul 1998 05:00:00 GMT' );
echo "Expires: Mon, 26 Jul 1998 05:00:00 ;"; header( 'Expires: Mon, 26 Jul 1978 05:00:00 GMT' );

想上面这样,浏览器访问,执行脚本,没有报错。(本以为汇报这样的错误:Warning: Cannot modify header information - headers already sent by)

此事何解:???

方法:

打开浏览器的调试控制台,发现 HTTP response header 中的Exipres 为 1978,顿时明白了。

联想到php 的输出缓冲的问题,在php 的配置文件中,有这么一段,看最后一句

; Output buffering is a mechanism for controlling how much output data
; (excluding headers and cookies) PHP should keep internally before pushing that
; data to the client. If your application's output exceeds this setting, PHP
; will send that data in chunks of roughly the size you specify.
; Turning on this setting and managing its maximum buffer size can yield some
; interesting side-effects depending on your application and web server.
; You may be able to send headers and cookies after you've already sent output
; through print or echo. You also may see performance benefits if your server is
; emitting less packets due to buffered output versus PHP streaming the output
; as it gets it. On production servers, 4096 bytes is a good setting for performance
; reasons.
; Note: Output buffering can also be controlled via Output Buffering Control
; functions.
; Possible Values:
; On = Enabled and buffer is unlimited. (Use with caution)
; Off = Disabled
; Integer = Enables the buffer and sets its maximum size in bytes.
; Note: This directive is hardcoded to Off for the CLI SAPI
; Default Value: Off
; Development Value: 4096
; Production Value: 4096
; http://php.net/output-buffering
output_buffering = 4096

4K字节的缓冲,这意味着  前面的这两个输出

header( 'Expires: Mon, 26 Jul 1998 05:00:00 GMT' );
echo "Expires: Mon, 26 Jul 1998 05:00:00 ;";

还缓冲在服务器中(我的是apache服务器,nginx服务器有是怎么样子呢,这个还不知道。。。),还没有通过http输出到浏览器,因此,后面的

header( 'Expires: Mon, 26 Jul 1978 05:00:00 GMT' );这个输出呢,就把缓冲区中的 响应头修改为了 1978。

另外:
  1、如果输出达到 4096(4k)字节时,服务器会立即将缓冲区中的内容 flush 出来,即立即输出给浏览器。
  2、关于缓冲区处理的一些列函数,php提供了 ob_flush(), ob_get_contents()等一系列函数 还有一点:当我们不确定 缓冲区中的内容是否已经有输出,那怎么办呢?
php 内置的函数 headers_sent() 可以用来判断,一些框架 如cakephp中就是这个来检查的!!
if (!headers_sent()) {
 header( 'Expires: Mon, 26 Jul 1978 05:00:00 GMT' ); }
这样就可以了,就确保不会报这样子的错 Warning: Cannot modify header information - headers already sent by

诡异的php 输出缓冲的更多相关文章

  1. PHP输出缓冲(Output Buffering)

    什么是缓冲区? 简单而言,缓冲区的作用就是,把输入或者输出的内容先放进内存,而不显示或者读取.至于为什么要有缓冲区,这是一个很广泛的问题~其实缓冲区最本质的作用就是,协调高速CPU和相对缓慢的IO设备 ...

  2. PHP 输出缓冲控制(Output Control) 学习

    php 缓冲简介 其实我对php ob 系列印象还是很模糊,具体怎么玩的,还不是很了解,平时curd,确实对这些内容没有深入.作为phper 甚是惭愧.网上搜了一通,互相copy,代码运行不能出现作者 ...

  3. php flush()刷新不能输出缓冲的原因分析

    在php程序编写中,flush()的使用率还是挺高的,它在网页表现即时信息效果时发挥了极为重要的作用,比如之前写的php实现限制文件下载速度的代码实例,flush()就起了举足轻重的作用,是进度条实现 ...

  4. PHP输出缓冲控制

    说到输出缓冲,首先要说的是一个叫做缓冲器(buffer)的东西.举个简单的例子说明他的作用:我们在编辑一篇文档时,在我们没有保存之前,系统是不会 向磁盘写入的,而是写到buffer中,当buffer写 ...

  5. PHP输出缓冲控制- Output Control 函数应用详解

    说到输出缓冲,首先要说的是一个叫做缓冲器(buffer)的东西.举个简单的例子说明他的作用:我们在编辑一篇文档时,在我们没有保存之前,系统是不会向磁盘写入的,而是写到buffer中,当buffer写满 ...

  6. perl的输出缓冲

      今天写一个小脚本的时候,需要即时输出当前进度到命令行上,并即时将重要数据写入报告文件中.但是perl默认是有输出缓冲的,显示到命令行上必须以\n结尾才行,输出到文件中,回车了都不行,非得要等缓冲区 ...

  7. PHP中刷新输出缓冲

    http://www.cnblogs.com/mutuan/archive/2012/03/18/2404957.html PHP中刷新输出缓冲buffer是一个内存地址空间,Linux系统默认大小一 ...

  8. PHP中输出缓冲

    在PHP中,当运行echo,print的时候,输出并没有马上通过tcp传给client浏览器显示, 而是将数据写入php buffer.php output_buffering机制,意味在tcp bu ...

  9. (转载)PHP中刷新输出缓冲

    (转载)http://www.cnblogs.com/mutuan/archive/2012/03/18/2404957.html PHP中刷新输出缓冲buffer是一个内存地址空间,Linux系统默 ...

随机推荐

  1. jQuery第十一章

    第十一章 一.jQuery性能优化 1.使用最新版本的jQuery类库. 2.使用合适的选择器 (1)$(“#id”) :使用id来定位DOM元素是最佳提高性能的方式. (2)$(“p”) :标签选择 ...

  2. 区块链Readme.md

    #Welcome to Azure Blockchain Projects This Repo host Azure related blockchain and distributed ledger ...

  3. Java Lambda表达式入门[转]

    原文链接: Start Using Java Lambda Expressions http://blog.csdn.net/renfufei/article/details/24600507 下载示 ...

  4. HTML通过事件传递参数到js 二 event

    原文链接http://blog.sina.com.cn/s/blog_8a18c33d010172pp.html event用例: <!DOCTYPE html><html>& ...

  5. opkg

    opkg是个安装器,小乔,功能全. root@hbg:/# opkg files opkgPackage opkg (9c97d5ecd795709c8584e972bfdf3aee3a5b846d- ...

  6. Marble 添加自定义Layer

    #include <marble/MarbleWidget.h> #include <marble/MarbleMap.h> #include <marble/Marbl ...

  7. LeetCode OJ 152. Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. Gym 100553B Burrito King 无脑背包

    题意就是你有n和m两个上限 吃一个东西会同时增加两个东西 m的值不能超过给定的m 问最后的n m值和每个东西吃了多少 贪心一下就好了 算一下性价比 从最大的开始吃 直到吃满了m n也一定是最大了 只是 ...

  9. 201312月CCF-2,ISBN号码分析

    明天要考CCF啦,偶还是很紧张的.最近看了数据结构,今天才开始上机练习,对,我就是这么懒..废话不多说,我写这篇文章主要是分析CCF编程的小窍门,因为在网上没找到,所以我决定自力更生丰衣足食.!!!! ...

  10. 【ADT】队列的基本C语言实现

    queue.h #ifndef _QUEUE_H_ #define _QUEUE_H_ #include <stdbool.h> #define MAXQUEUE 10 // 定义队列类型 ...