看到“无法修改标题信息”尝试重定向时出错 [英] Seeing "Cannot modify header information" error when attempting to redirect

查看:106
本文介绍了看到“无法修改标题信息”尝试重定向时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的PHP mail()脚本,向用户发送他们刚刚提交的表单的详细信息。 PHP将所有数据插入到数据库中,但是当数据处理完成后,我想重定向它们时出现问题。

I have a basic PHP mail() script which e-mails users the details of a form they've just submitted. The PHP inserts all the data into the database fine, but there's a problem when I want to redirect them after the data has finished processing.

我目前正在使用: / p>

I'm currently using:

mail($email, $subject, $message, $headers);

header('Location: '/reservations/?res='.$res.'&id='.$id.'');

但是它抛出了这个错误:

But it's throwing up this error:

Warning: Cannot modify header information - headers already sent

我意识到您不能发送多个标题,但是是否有任何区分邮件和位置标题的方法?我不想使用JavaScript重定向,因为它太慢,滞后。我读了一些关于 ob_start()的地方,但我不知道这是否需要...

I realise you can't send multiple headers, but is there any way of differentiating between the mail and location headers? I don't want to use JavaScript redirect because it's too slow and lags. I read somewhere about ob_start() but I don't know if that's necessary...

推荐答案

使用 标头修改HTTP标头 要求它尚未发送。这样就发生在第一次输出了。

Modifying the HTTP header with header requires that it has not been sent yet. And that happens when the first output happened.

所以为了避免发送HTTP头,你需要在调用头之前避免任何输出。 c $ c>或者您需要缓冲它,以便它不被发送(请参阅输出控件,特别是 ob_start )。

So to avoid the HTTP header being sent you need to avoid any output before calling header or you need to buffer it so that it doesn’t get sent (see output control, especially ob_start).

输出发生的导致发送HTTP头的位置在错误消息中(从文件开始输出:) 。

The position where the output happened that caused sending the HTTP header is in the error message ("output started at file‍:‍line").

这篇关于看到“无法修改标题信息”尝试重定向时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆