简体   繁体   English

Codeigniter 错误信息无法修改头信息

[英]Codeigniter error message Cannot modify header information

I got this error message when i run my php CodeIgniter project:当我运行我的 php CodeIgniter 项目时,我收到此错误消息:

A PHP Error was encountered遇到 PHP 错误

Severity: Warning严重性:警告

Message: Cannot modify header information - headers already sent by (output started at C:\\AppServ\\www\\tree\\application\\models\\mtree.php:17)消息:无法修改标头信息 - 标头已发送(输出开始于 C:\\AppServ\\www\\tree\\application\\models\\mtree.php:17)

Filename: core/Common.php文件名:core/Common.php

Line Number: 442行号:442

line 17:第 17 行:

echo $row->members_id . "</br>";

this is my model function:这是我的模型函数:

function convert_all() {
    $this->db->empty_table('table1');
    $this->db->empty_table('table2');
    $this->db->query("INSERT INTO `table1` (`members_id`, `members_username`);
    $query = $this->db->get('table2');
    foreach ($query->result() as $row) {
        if ($row->members_id > 1) {
            echo $row->members_id . "</br>";
        }
        if ($this->isadvanced($row->members_id, $row->members_direct_id)) {
            $this->insert_to_right($row, $row->members_direct_id);
        } else {
            $this->insert_to_left($row, $row->members_direct_id);
        }
    }
}

While using codeigniter, its best recommended that you echo only in views.在使用笨,其最佳建议您echo只在视图中。

Echoing anywhere randomly sends some data to browser, after which you can't modify headers (this includes attempt to redirect, set content-type, etc)在任何地方回显随机向浏览器发送一些数据,之后您无法修改标题(这包括尝试重定向、设置内容类型等)

You should re-organize your code, such that, echo are done within views only.您应该重新组织您的代码,以便echo仅在视图中完成。 This will solve your header issues in Codeigniter.这将解决您在 Codeigniter 中的标题问题。

This also includes extra whitespaces at the end after the closing brace " } ?> " in non-view php files.这还包括在非视图 php 文件中右大括号“ } ?> ”后面的额外空格。

You can't display anything before the header function is called.在调用 header 函数之前不能显示任何内容。 You should not display anything form model or controller to avoid this kind of error.你不应该显示任何形式的模型或控制器以避免这种错误。 If you want to display something form the controller or model then you should store the output to the buffer instead of sending it to the browser.如果您想从控制器或模型中显示某些内容,那么您应该将输出存储到缓冲区而不是将其发送到浏览器。 You can store the output to the buffer using ob_start() function before header() function is called.您可以在调用header()函数之前使用ob_start()函数将输出存储到缓冲区。 Then you can do something like this然后你可以做这样的事情

if(true)
{
    header("Location:http://google.com");
}
else
{
   ob_end_flush();
}

this was your issue - echo $row->members_id .这是你的问题 - echo $row->members_id 。 ""; ""; -- as the above answers say, don't echo something in your controllers, push to do it in the views - always - 正如上面的答案所说,不要在你的控制器中回显一些东西,在视图中推动它 - 总是

I think it will be helpfull Use $config['sess_save_path'] = sys_get_temp_dir();我认为它会有所帮助 使用$config['sess_save_path'] = sys_get_temp_dir(); instead of $config['sess_save_path'] = NULL;而不是$config['sess_save_path'] = NULL; in your Application/config/config.php file在您的Application/config/config.php文件中

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM