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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算