dqjo4340 2016-01-06 04:18
浏览 42
已采纳

为什么我没有收到“已发送标头”错误? [重复]

This question already has an answer here:

I have code like this:

<!DOCTYPE html>
<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    $cookie_name = "user";
    $cookie_value = "John Doe";

    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

?>
<html>
    <body>

        <?php
        if (!isset($_COOKIE[$cookie_name])) {
            echo "Cookie named '" . $cookie_name . "' is not set!";
        } else {
            echo "Cookie '" . $cookie_name . "' is set!<br>";
            echo "Value is: " . $_COOKIE[$cookie_name];
        }
        ?>

    </body>
</html>

and from my knowlege it should return warning

Cannot modify header information - headers already sent by

As in this question How to fix "Headers already sent" error in PHP but I don't get any warning and the cookie is set. Why is that? Is php added some kind of cache and now you can send headers after the text is sent? I'm using php 5.6.11.

</div>

展开全部

  • 写回答

1条回答 默认 最新

  • doujie4050 2016-01-06 04:23
    关注

    If you use output_buffering = On in your php.ini you can send cookies after the headers have been sent.

    This is noticed in php.ini comment :

    Output buffering allows you to send header lines (including cookies) even after you send body content, at the price of slowing PHP's output layer a bit. You can enable output buffering during runtime by calling the output buffering functions. You can also enable output buffering for all files by setting this directive to On. If you wish to limit the size of the buffer to a certain size - you can use a maximum number of bytes instead of 'On', as a value for this directive (e.g., output_buffering=4096).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?