PHP 标头位置重定向不起作用
include 'header.php';
// ... some code
header('Location:index.php');
exit;
上面的代码一直给我带来重定向问题。 错误如下:
警告:无法修改标头信息 - 标头已由(输出 开始于 /Applications/MAMP/htdocs/testygubbins/OO/test/header.php:15) /Applications/MAMP/htdocs/testygubbins/OO/test/form.php 第 16 行。
我应该做什么才能使其工作?
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">
include 'header.php';
// ... some code
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.
What should I be doing to make it work?
header.php code:
<?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">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可能在 php 文件中的某处有一些被解释为脚本输出的“纯文本”。 它甚至可能是 php 脚本标记说明符之前或之后的换行符(小于+问号+“php”)。
此外,如果我没记错的话,根据http规范,“Location”标头只接受完整的URL,而不接受相对位置。 也要记住这一点。
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").
Besides, if I remember correctly, according to http specification, the "Location" header accepts only full URLs, not relative locations. Have that in mind too.
不要包含 header.php。 当您要重定向时,不应输出 HTML。
制作一个新文件,例如。 “pre.php”。 将其放入其中:
然后在 header.php 中包含该文件,而不是包含其他两个文件。
在 form.php 中,包含 pre.php 而不是 header.php。
Don't include header.php. You should not output HTML when you are going to redirect.
Make a new file, eg. "pre.php". Put this in it:
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.
您的 include 会生成输出,从而导致稍后无法发送 http 标头。 两个选项:
我建议您学习第二个选项,因为它使您更加灵活。
Your include produces output, thereby making it impossible to send a http header later. Two option:
I would recommend you learn the second option, as it makes you far more flexible.
另请参阅您的 php 文件文本编码。 我的是
UTF-8
和BOM
,它阻止了脚本的工作。 但现在删除BOM
后可以完美地工作...Also see your php file text encoding. Mine was
UTF-8
withBOM
and it prevented the script to work. But now works flawlessly after removing theBOM
...尝试使用 JavaScript 进行重定向:
Try redirection with JavaScript:
仔细查看您的包含内容 - 也许您在结束后有一个空行?> ?
这将导致一些文字空白作为输出发送,从而阻止您进行后续的标头调用。
请注意,离开收盘价是合法的吗?> 关闭包含文件,这是避免此问题的有用习惯用法。
(编辑:查看您的标头,如果您想输出标头,则需要避免执行任何 HTML 输出,或者使用输出缓冲来捕获它)。
最后,如 PHP header 手册页 指出,您确实应该使用完整的 URL 来重定向:
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.
(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).
Finally, as the PHP manual page for header points out, you should really use full URLs to redirect:
或者,不要考虑文件中某处的换行符或空格,您可以缓冲输出。 基本上,您在文件的开头调用
ob_start();
,在文件末尾调用ob_end_flush();
。 您可以在php.net ob-start函数说明中找到更多详细信息。编辑:
如果使用缓冲,则可以在 header() 函数之前和之后输出 HTML - 缓冲将忽略输出并仅返回重定向标头。
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 andob_end_flush();
at the end. You can find more details at php.net ob-start function description.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.
尝试这个 :
Try This :
如果我理解正确的话,某些内容已经从 header.php 发出(可能是一些 HTML),因此标头已设置。 您可能需要重新检查 header.php 文件中可能在第一个文件之前输出 HTML 或空格的任何部分
编辑:我现在确信它是由 header.php 引起的,因为您有这些 HTML 输出。 您可以通过删除“include('header.php');”来解决此问题 行并将以下代码复制到您的文件中。
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
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.