为什么ob_start()必须在session_start()之前才能在PHP中工作? [英] why ob_start() must come ahead of session_start() to work in PHP?

查看:105
本文介绍了为什么ob_start()必须在session_start()之前才能在PHP中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这不合理.

为什么实际上是这样的规则?

Why is it actually such a rule?

推荐答案

在"正常情况"中,我认为不必在session_start之前调用ob_start,也不反之亦然.

In the "normal case", I don't think ob_start has to be called before session_start -- nor the other way arround.

尽管引用 session_start 的手册页,但是:

Quoting the manual page of session_start, though :

session_start()将在内部注册 URL重写时的输出处理程序 trans-sid已启用.如果用户使用 ob_gzhandler或类似的ob_start() 输出处理程序的顺序是 对于正确的输出很重要.为了 例如,用户必须注册 会话开始前先使用ob_gzhandler.

session_start() will register internal output handler for URL rewriting when trans-sid is enabled. If a user uses ob_gzhandler or like with ob_start(), the order of output handler is important for proper output. For example, user must register ob_gzhandler before session start.

但这是一种特殊情况:这里的输出处理程序的顺序很重要:如果您希望一个处理程序修改另一个处理程序的操作,则必须在正确的"位置执行它们订单.

But this is some kind of a special case : the thing is, here, that the order of output handlers is important : if you want one handler to modify things the other did, they have to be executed in the "right" order.


通常,如果您不使用此类处理程序(Apache和 mod_deflate 在压缩输出方面表现出色,例如),唯一重要的是在调用session_start 之前不得发送标头(因为,具体取决于您的配置,session_start发送cookie,并将其作为HTTP标头传递).


Generally, if you don't use that kind of handlers (Apache and mod_deflate do a great job when it comes to compressing output, for instance), the only thing that matters is that headers must not be sent before you call session_start (because, depending on your configuration, session_start sends cookies, which are passed as HTTP headers).

只要要发送任何数据,就发送标头-即,只要有任何输出,甚至是<?php ?>标记之外的一个空格:

And headers are sent as soon as any piece of data has to be sent -- ie, as soon as there is any output, even one whitespace outside of <?php ?> tags :

注意:如果您使用的是基于Cookie的 会话,您必须致电 session_start()在任何事情之前 输出到浏览器.

Note: If you are using cookie-based sessions, you must call session_start() before anything is outputted to the browser.

ob_start表示PHP必须缓冲数据:

ob_start indicates that PHP has to buffer data :

此功能将打开输出 缓冲.输出缓冲时 处于活动状态,没有从发送任何输出 脚本(标头除外)代替 输出存储在内部 缓冲.

This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

这样,在您实际说出"发送数据"之前,不会发送输出.这意味着标头不会立即发送-这意味着session_start可以在以后调用,即使应该已经输出,即使没有使用ob_start.

This way, output is not sent before you actually say, yourself, "send the data". This means headers are not send immediatly -- which means session_start can be called later, even if there should have been output, if ob_start had not been used.


希望这会使事情变得更清楚...

这篇关于为什么ob_start()必须在session_start()之前才能在PHP中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆