为什么我没有获得“头已经发送”错误? [英] Why I don't get "headers already sent" error?

查看:120
本文介绍了为什么我没有获得“头已经发送”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

 <!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天$ ​​b
$ b?>
< html>
< body>

<?php
if(!isset($ _ COOKIE [$ cookie_name])){
echoCookie named'。 $ cookie_name。 没有设置!
} else {
echoCookie'。 $ cookie_name。 '已设置!< br>;
echoValue is:。 $ _COOKIE [$ cookie_name];
}
?>

< / body>
< / html>

从我的知识中应该会返回警告



<无法修改标题信息 -
发送的标题

如此问题如何解决 ;头已经发送错误在PHP
但我没有得到任何警告,并设置了cookie。这是为什么?是php添加某种缓存,现在你可以发送标题后发送文本?我使用的是php 5.6.11。

解决方案

如果你使用 output_buffering = On php.ini 中,您可以在标题发送后发送Cookie。



in php.ini comment:


输出缓冲允许你发送标题行(包括cookie)
, ,以减慢PHP的
输出层的代价。您可以通过
调用输出缓冲功能在运行时启用输出缓冲。您还可以通过将此指令设置为On来为所有文件启用输出
缓冲。如果你希望
将缓冲区的大小限制到一个特定的大小 - 你可以使用
最大字节数而不是'On'作为这个
指令的值(例如, output_buffering = 4096)。



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.

解决方案

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).

这篇关于为什么我没有获得“头已经发送”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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