php适合页脚的代码,PHP代码是在页眉还是页脚中是否重要?

最终,这取决于.我使用的大多数PHP托管机制都会以块的形式传输响应,因为它是从脚本中收到的,完全省略了Content-Length头(因为事先不知道).您可以定期使用

flush()刷新响应,以强制服务器将目前缓冲的内容传输到客户端.

所以,如果你要做的事情需要花费很多时间并且想要让页面提前渲染,那么正确的方法就是输出尽可能多的页面,调用flush(),然后做你昂贵的任务.请确保不要超过声明的最大PHP脚本持续时间.

好的,所以这个解释确实有点过于简单:

实际上可以在某些情况下发送Content-Length标头.例如,如果脚本的响应小于PHP的发送数据缓冲区,和/或如果脚本需要一定的时间来执行,那么服务器将确切地知道内容的长度并且可以添加长度标头.

此外,flush()实际上可能没有做任何事情.这取决于您的服务器配置和其他因素.请特别注意有关服务器端行为的文档中的这些警告:

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn’t affect PHP’s userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.

Several servers, especially on Win32, will still buffer the output from your script until it terminates before transmitting the results to the browser.

Server modules for Apache like mod_gzip may do buffering of their own that will cause flush() to not result in data being sent immediately to the client.

换句话说:测试,测试,测试.确保Web服务器不会干扰您尝试实现的行为.必要时重新配置Web服务器.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值