dongxue163536 2017-09-17 11:03
浏览 37
已采纳

发送输出后调用header()

According to the PHP documentation:

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

but when I tried the example that the documentation reports (with a little change):

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('X-Header: http://www.example.com/');
exit;
?>

all worked just fine, no error poped up and I smoothly got my <html> tag in the output and my X-Header in the headers.

I'm using PHP 7.1.9, so is still correct what the documentation says?

  • 写回答

2条回答 默认 最新

  • douzuo0711 2017-09-17 11:23
    关注

    The documentation is still correct.

    For performance purposes, the interpreter puts the output in a buffer. When the buffer is filled for the first time, it sends the headers before sending the content of the buffer.

    This lets the script produce a small amount of output before sending the headers.

    Read more about output buffering configuration settings.

    The option output_buffering allows turning the feature off or on and even setting the size of the buffer.

    The option implicit_flush tells the interpreter to flush the buffer after every output block. This forces your script to send the headers correctly, before any output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题