chapter12.php
<?php  
  ob_start();
  $exam = "我喜欢PHP";  
setcookie("text",$exam);
setcookie("text",$exam,time()+1800);
ob_end_flush();
?>   
<a href="exam.php">输出Cookie</a>
exam.php
<?php
  if(!empty($_COOKIE[text])){
  echo "COOKIE值为:".$_COOKIE[text]."<p>";
echo "<a href='exam.del.php'>删除Cookie</a>";
  }
  else{
echo "Cookie已被删除<P>";
echo "<a href='chapter12.php'>回首页</a>";
  }
  echo $_COOKIE[text];
?>运行chapter12.php时,为什么老是跳出Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\chapter12.php:9) in C:\AppServ\www\chapter12.php on line 12
Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\chapter12.php:9) in C:\AppServ\www\chapter12.php on line 13
输出Cookie 错误

解决方案 »

  1.   

    chapter12.php
    <?php  
      ob_start();
      $exam = "我喜欢PHP";  
    setcookie("text",$exam);
    setcookie("text",$exam,time()+1800);
    <a href="exam.php">输出Cookie</a>
    ob_end_flush();
    ?>  <?php ?>外面没有空格空行试试
      

  2.   

    chapter12.php
      这个页面的header 之前有输出...所以报警告.
     
    如果要想支持header 之前也可以输出而且不报错. 可以修改php.ini : output_buffering = 4096
      

  3.   

    复制了
    chapter12.php
    <?php  
      ob_start();
      $exam = "我喜欢PHP";  
    setcookie("text",$exam);
    setcookie("text",$exam,time()+1800);
    ob_end_flush();
    ?>  
    <a href="exam.php">输出Cookie</a>
    没有错误
    而且代码只有8行没出现 错误中12 行和13行