php PHP标头重定向不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/423860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Header redirect not working
提问by Drew
include('header.php');
$name = $_POST['name'];
$score = $_POST['score'];
$dept = $_POST['dept'];
$MyDB->prep("INSERT INTO demo (`id`,`name`,`score`,`dept`, `date`) VALUES ('','$name','$score','$dept','$date')");
// Bind a value to our :id hook
// Produces: SELECT * FROM demo_table WHERE id = '23'
$MyDB->bind(':date', $date);
// Run the query
$MyDB->run();
header('Location:index.php');
exit;
The above code keeps giving me an issue with the redirect. The error is the following:
上面的代码一直给我一个重定向问题。错误如下:
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/testygubbins/OO/test/header.php:15) in /Applications/MAMP/htdocs/testygubbins/OO/test/form.php on line 16.
警告:无法修改标题信息 - 标题已由 /Applications/MAMP/htdocs/testygubbins/OO/test/form 中的(输出开始于 /Applications/MAMP/htdocs/testygubbins/OO/test/header.php:15)发送。 php 在第 16 行。
I am totally flummoxed by this. Does anyone know what I should be doing to make it work?
我完全被这弄糊涂了。有谁知道我应该怎么做才能让它发挥作用?
EDIT
编辑
header.php code:
header.php 代码:
<?php
include('class.user.php');
include('class.Connection.php');
$date = date('Y-m-j');
?>
<html>
<head>
<link rel=StyleSheet href="css/style.css" type="text/css" media=screen>
<title>Test</title>
</head>
<body>
<div id="page">
回答by Paul Dixon
Look carefully at your includes - perhaps you have a blank line after a closing ?> ?
仔细查看您的包含 - 也许您在结束后有一个空行?> ?
This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls.
这将导致一些文字空白作为输出发送,阻止您进行后续标题调用。
Note that it is legal to leave the close ?> off the include file, which is a useful idiom for avoiding this problem.
请注意,在包含文件中保留 close ?> 是合法的,这是避免此问题的有用习惯用法。
(EDIT: looking at your header, you need to avoid doing any HTML output if you want to output headers, or use output buffering to capture it).
(编辑:查看您的标头,如果您想输出标头或使用输出缓冲来捕获它,则需要避免执行任何 HTML 输出)。
Finally, as the PHP manual page for headerpoints out, you should really use full URLs to redirect:
最后,正如header的PHP 手册页指出的那样,您确实应该使用完整的 URL 来重定向:
Note: HTTP/1.1 requires an absolute URI as argument to Location:including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:
注意:HTTP/1.1 需要一个绝对 URI 作为Location 的参数:包括方案、主机名和绝对路径,但一些客户端接受相对 URI。您通常可以使用 $_SERVER['HTTP_HOST']、$_SERVER['PHP_SELF'] 和 dirname() 自己从相对的 URI 中创建绝对 URI:
回答by T.Todua
COMMON PROBLEMS:
常见问题:
1)There should be NO output (i.e. echo...or HTML parts) before the header(...);command.
1)echo...在header(...);命令之前应该没有输出(即或 HTML 部分)。
2)After header(...);you must use exit();
2)在header(...);你必须使用之后exit();
3)Remove any white-space(or newline) before <?phpand after ?>tags.
3)删除任何空白(或换行之前)<?php和后?>标记。
4)Check that php file (and also other .phpfiles, that are included) -
they should have UTF8 without BOMencoding (and not just UTF-8). Because default UTF8adds invisible character in the start of file (called "BOM"), so you should avoid that !!!!!!!!!!!
4)检查该 php 文件(以及其他.php文件,即included)-它们应该具有没有 BOM编码的UTF8(而不仅仅是UTF-8)。因为默认的UTF8在文件的开头添加了不可见的字符(称为“ BOM”),所以你应该避免这种情况!!!!!!!!!!!!
5)Use 301 or 302 reference:
5)使用 301 或 302 参考:
header("location: http://example.com", true, 301 ); exit;
6)Turn on error reporting. And tell the error.
6)打开错误报告。并告诉错误。
7)If none of above helps, use JAVASCRIPT redirection (however, discouraged method), may be the last chance in custom cases...:
7)如果以上都没有帮助,请使用 JAVASCRIPT 重定向(但是,不鼓励的方法),这可能是自定义情况下的最后机会......:
echo "<script type='text/javascript'>window.top.location='http://website.com/';</script>"; exit;
回答by ya23
Alternatively, not to think about a newline or space somewhere in the file, you can buffer the output. Basically, you call ob_start()at the very beginning of the file and ob_end_flush()at the end. You can find more details at php.net ob-start function description.
或者,不要考虑文件中某处的换行符或空格,您可以缓冲输出。基本上,您ob_start()在文件的开头和ob_end_flush()结尾调用。您可以在php.net ob-start 函数描述中找到更多详细信息。
Edit:If you use buffering, you can output HTML before and after header() function - buffering will then ignore the output and return only the redirection header.
编辑:如果使用缓冲,则可以在 header() 函数之前和之后输出 HTML - 缓冲将忽略输出并仅返回重定向标头。
回答by mostafa
Try This :
尝试这个 :
**ob_start();**
include('header.php');
$name = $_POST['name'];
$score = $_POST['score'];
$dept = $_POST['dept'];
$MyDB->prep("INSERT INTO demo (`id`,`name`,`score`,`dept`, `date`) VALUES ('','$name','$score','$dept','$date')");
// Bind a value to our :id hook
// Produces: SELECT * FROM demo_table WHERE id = '23'
$MyDB->bind(':date', $date);
// Run the query
$MyDB->run();
header('Location:index.php');
**ob_end_flush();**
exit;
回答by myplacedk
Look at /Applications/MAMP/htdocs/testygubbins/OO/test/header.php line 15.
查看 /Applications/MAMP/htdocs/testygubbins/OO/test/header.php 第 15 行。
At that position, it makes some output. Fix it. :)
在那个位置,它会产生一些输出。修理它。:)
回答by Gant
If I understand correctly, something has already sent out from header.php (maybe some HTML) so the headers have been set. You may need to recheck your header.php file for any part that may output HTML or spaces before your first
如果我理解正确,一些东西已经从 header.php(也许是一些 HTML)发出,所以标题已经设置好了。您可能需要重新检查 header.php 文件中可能输出 HTML 或空格的任何部分
EDIT:I am now sure that it is caused from header.php since you have those HTML output. You can fix this by remove the "include('header.php');" line and copy the following code to your file instead.
编辑:我现在确定它是由 header.php 引起的,因为你有那些 HTML 输出。您可以通过删除“include('header.php');”来解决此问题 行并将以下代码复制到您的文件中。
include('class.user.php');
include('class.Connection.php');
$date = date('Y-m-j');
回答by macbirdie
You may have some "plain text" somewhere in php files that is interpreted as script output. It may be even a newline before or after the php script tag specifier (less-than + question mark + "php").
您可能在 php 文件的某处有一些“纯文本”,被解释为脚本输出。它甚至可能是 php 脚本标记说明符之前或之后的换行符(小于 + 问号 + “php”)。
Besides, if I remember correctly, according to http specification, the "Location" header accepts only full URLs, not relative locations. Have that in mind too.
此外,如果我没记错的话,根据 http 规范,“Location”标头只接受完整的 URL,而不接受相对位置。也要记住这一点。
回答by myplacedk
Don't include header.php. You should not output HTML when you are going to redirect.
不要包含 header.php。当您要重定向时,您不应该输出 HTML。
Make a new file, eg. "pre.php". Put this in it:
制作一个新文件,例如。“pre.php”。把这个放进去:
<?php
include('class.user.php');
include('class.Connection.php');
?>
Then in header.php, include that, in stead of including the two other files. In form.php, include pre.php in stead of header.php.
然后在 header.php 中,包含它,而不是包含其他两个文件。在 form.php 中,包含 pre.php 而不是 header.php。
回答by Martijn Heemels
Your include produces output, thereby making it impossible to send a http header later. Two option:
您的包含产生输出,因此以后无法发送 http 标头。两种选择:
- Move the output somewhere after the include.
- Use output buffering, i.e. at the very start of your script, put ob_start(), and at the end, put ob_flush(). This enables PHP to first wait for all the output to be gathered, determine in what order to render it, and outputs it.
- 将输出移动到包含之后的某处。
- 使用输出缓冲,即在脚本的最开始,放置 ob_start(),最后放置 ob_flush()。这使 PHP 能够首先等待收集到的所有输出,确定以什么顺序呈现它,然后输出它。
I would recommend you learn the second option, as it makes you far more flexible.
我建议您学习第二个选项,因为它使您更加灵活。
回答by Napster
also try include_once() instead of include() that can also work
也尝试 include_once() 而不是 include() 也可以工作

