简体   繁体   English

PHP - 无法修改标头信息 - 标头已由(输出开始于

[英]PHP - cannot modify header information - headers already sent by (output started at

I am working with PHP and I got this message我正在使用 PHP,我收到了这条消息

cannot modify header information - headers already sent by (output started at无法修改标头信息 - 标头已由(输出开始于

after executing a code which looks like this:执行如下代码后:

<?php
    if (condition) {

        if (condition) {
            //Statement
        }

        $to = $_POST['email'];
        $subject = "Registration Confirmation";

        $message = '
                <html>
                    <head>
                        <title>'.$subject.'</title>
                    </head>
                </html>
            ';

        // Always set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

        // More headers
        $headers .= 'From: <contact@ghanalifestore.com>' . "\r\n";

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

        header('Location: reset.php?username='.$username);
        exit();

    }else {
        //statement
    }

Based on this answer from Stackoverflow , I think the error is coming from the HTML code in the variable $message .根据Stackoverflow的这个答案,我认为错误来自变量$message中的 HTML 代码。 I don't really know how I can modify the content of that variable to avoid the error.我真的不知道如何修改该变量的内容以避免错误。

Kindly help me solve this problem.请帮我解决这个问题。

ob_start(); ob_start();

html is also sending output that is why this is happening so use ob_start(); html 也在发送输出,这就是发生这种情况的原因,所以使用 ob_start(); in top of your code, hope this solves your problem.在您的代码之上,希望这可以解决您的问题。

From php documentation :来自php 文档

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.请记住,必须在发送任何实际输出之前调用 header(),无论是通过普通 HTML 标记、文件中的空白行还是从 PHP 发送。

So move header('Location: reset.php?username='.$username);所以移动header('Location: reset.php?username='.$username); before any other kind of output.在任何其他类型的输出之前。

By the way, this question was already answered:顺便说一句,这个问题已经回答了:

just change your code to echo'window.location="dashboard.php?id=40";';只需将代码更改为 echo'window.location="dashboard.php?id=40";'; then it will work然后它会工作

change your code:-更改您的代码:-
echo'window.location="username='.$username";'; echo'window.location="username='.$username";';
exit();出口();

Only writing the following code, solved my problem. 只写下面的代码,解决了我的问题。 This problem appeared in cpanel hosting and not in other hosting 此问题出现在cpanel托管中,而不是在其他托管中

I pulled my hair out trying to find this issue in PHP code only to find that clearing the cache (Firefox) removed the error.我试图在 PHP 代码中找到这个问题,却发现清除缓存(Firefox)消除了错误。 If you are changing code, copying files, etc. your browser could get very confused so whenever you have an error like this or any error that involves how the code is read and you are observing irregularities on the screen or confusing log errors be sure to clear the cache and restart your browser as a possible remedy.如果您正在更改代码、复制文件等,您的浏览器可能会变得非常混乱,因此每当您遇到此类错误或任何涉及代码读取方式的错误并且您观察到屏幕上的不规则或令人困惑的日志错误时,请务必清除缓存并重新启动浏览器作为可能的补救措施。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP 重现警告“无法修改 header 信息 - 标头已由(输出开始于...”与 XAMPP 8..1.1 - PHP reproduce warning "Cannot modify header information - headers already sent by (output started at... " with XAMPP 8..1.1 警告:无法修改已发送的标头信息标头(输出始于(/themes/kaboodle/functions/admin-setup.php - Warning:Cannot modify header information-headers already sent by (output started at (/themes/kaboodle/functions/admin-setup.php 为什么收到警告:无法修改标头信息-标头已经发送过(输出从...开始)? - Why am I getting Warning: Cannot modify header information - headers already sent by (output started at …)? 在CSV下载期间-警告:无法修改标头信息-已发送的标头(输出始于 - During CSV downloading - Warning: Cannot modify header information - headers already sent by (output started at 无法修改输出已发送的标题信息标题 - Cannot modify header information headers already sent by output PHP 5.4无法修改标题信息-标题已发送 - Php 5.4 Cannot modify header information - headers already sent 无法修改标头信息-标头已经由PHP发送 - Cannot modify header information - headers already sent by: PHP PHP-SQLite问题:无法修改标头信息-标头已发送 - PHP - SQLite Issue: Cannot modify header information - headers already sent by 无法修改已发送的标头信息标头 - Cannot Modify Header Information Headers Already Sent 无法修改标题信息-标题已发送 - Cannot modify Header Information - headers already sent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM