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?
&lt;?php
require'leader.php';
require'connection.php'; \ n
mysql_query(“DELETE FROM引用WHERE ID = $ _GET [id]”)或死(“没有正确删除”);
header('Location:index.php');
?&gt;
code > pre>
当我运行时,它说:
警告:无法修改标题信息 - 已经发送的标题(输出从第6行的xxxxx开始 p>
有什么问题? p>
div>
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.
Your php code is sending content from included files before header data from header('Location: index.php')
. HTTP headers must always be sent prior to any other content.
And please for your own sake, use parametrized queries.
You're probably echo/printing something out in header.php
or your die
Which sends header information prior to your call for header('Location: index.php');
The headers must be the first thing sent by your script. If there is any output to the page before you call the header() function then you will get that error.