PHP 标头位置重定向不起作用

发布于 2024-07-11 05:12:59 字数 693 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

梦中楼上月下 2024-07-18 05:13:00

您可能在 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.

何以畏孤独 2024-07-18 05:13:00

不要包含 header.php。 当您要重定向时,不应输出 HTML。

制作一个新文件,例如。 “pre.php”。 将其放入其中:

<?php
include('class.user.php');
include('class.Connection.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:

<?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.

终止放荡 2024-07-18 05:13:00

您的 include 会生成输出,从而导致稍后无法发送 http 标头。 两个选项:

  1. 将输出移动到包含之后的某个位置。
  2. 使用 输出缓冲,即在脚本的最开始处,放置ob_start(),最后放ob_flush()。 这使得 PHP 能够首先等待收集所有输出,确定以什么顺序呈现它,然后输出它。

我建议您学习第二个选项,因为它使您更加灵活。

Your include produces output, thereby making it impossible to send a http header later. Two option:

  1. Move the output somewhere after the include.
  2. 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.

I would recommend you learn the second option, as it makes you far more flexible.

靑春怀旧 2024-07-18 05:13:00

另请参阅您的 php 文件文本编码。 我的是 UTF-8BOM,它阻止了脚本的工作。 但现在删除 BOM 后可以完美地工作...

Also see your php file text encoding. Mine was UTF-8 with BOM and it prevented the script to work. But now works flawlessly after removing the BOM...

梦萦几度 2024-07-18 05:13:00

尝试使用 JavaScript 进行重定向:

<script type="text/javascript">
  window.location.href='index.php';
</script>

Try redirection with JavaScript:

<script type="text/javascript">
  window.location.href='index.php';
</script>
独自←快乐 2024-07-18 05:12:59

仔细查看您的包含内容 - 也许您在结束后有一个空行?> ?

这将导致一些文字空白作为输出发送,从而阻止您进行后续的标头调用。

请注意,离开收盘价是合法的吗?> 关闭包含文件,这是避免此问题的有用习惯用法。

(编辑:查看您的标头,如果您想输出标头,则需要避免执行任何 HTML 输出,或者使用输出缓冲来捕获它)。

最后,如 PHP header 手册页 指出,您确实应该使用完整的 URL 来重定向:

注意:HTTP/1.1 需要绝对
URI 作为位置的参数:
包括方案、主机名和
绝对路径,但有些客户端接受
相对 URI。 通常你可以使用
$_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] 和 dirname() 到
从相对 URI 生成绝对 URI
一个你自己:

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:

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:

把时间冻结 2024-07-18 05:12:59

或者,不要考虑文件中某处的换行符或空格,您可以缓冲输出。 基本上,您在文件的开头调用 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 and ob_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.

揽月 2024-07-18 05:12:59

尝试这个 :

ob_start();

include 'header.php';
// ... some code
header('Location:index.php');

ob_end_flush();
exit;

Try This :

ob_start();

include 'header.php';
// ... some code
header('Location:index.php');

ob_end_flush();
exit;
心房敞 2024-07-18 05:12:59

如果我理解正确的话,某些内容已经从 header.php 发出(可能是一些 HTML),因此标头已设置。 您可能需要重新检查 header.php 文件中可能在第一个文件之前输出 HTML 或空格的任何部分

编辑:我现在确信它是由 header.php 引起的,因为您有这些 HTML 输出。 您可以通过删除“include('header.php');”来解决此问题 行并将以下代码复制到您的文件中。

include('class.user.php');
include('class.Connection.php');

        $date = date('Y-m-j');

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.

include('class.user.php');
include('class.Connection.php');

        $date = date('Y-m-j');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文