douyun8901 2010-12-09 16:49
浏览 15
已采纳

PHP标头错误为什么这不起作用?

<?php
require 'header.php';
require 'connection.php';

mysql_query("DELETE FROM quotes WHERE ID = $_GET[id]") or die("didnt delete properly");
header('Location: index.php');
?>

When I run this it says: Warning: Cannot modify header information - headers already sent by (output started at xxxxx on line 6

What is wrong?

  • 写回答

4条回答 默认 最新

  • dongwen3410 2010-12-09 16:53
    关注

    Either header.php or connection.php are outputting some content. You cannot modify the HTTP headers after content is sent, because the headers have already been sent at that point.

    A hack of a solution would be this:

    <?php
    
    ob_start();
    
    require 'header.php';
    require 'connection.php';
    
    mysql_query("DELETE FROM quotes WHERE ID = $_GET[id]") or die("didnt delete properly");
    
    header('Location: index.php');
    
    ob_end_flush();
    
    ?>
    

    However, you should instead figure out where content is being sent and suppress it, or reorder it to come after the header() call.

    If, as I suspect, header.php outputs an HTML header, you can just eliminate the require 'header.php'; line -- the content will never be shown anyway, since this is a redirect.

    Also, note that the HTTP standard requires that the value of a Location header be an absolute URL. Therefore, header('Location: index.php'); will generate an HTTP response that is invalid according to the HTTP standard.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?