如何修复错误“Cannot modify header information - headers already sent by”

3

我在codeigniter中遇到了以下类型的错误,我无法解决它。有没有人知道这个错误的原因。

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at    /home/samples1/public_html/venture-test/application/controllers/admin/admin_notifications.php:158)

Filename: helpers/url_helper.php

Line Number: 540

我使用了两种方法,它们都在同一个控制器中,如下所示。
 <?php
    public function approve_recent_user() {  
    $user_id = $this->uri->segment(4);
    $notification_id = $this->uri->segment(5);
    $status = 1;
    if($this->admin_notification_db->user_approve_flag($user_id,$status) == true) {
     redirect('admin/admin_notifications/unapproved_users');                
    }
    else {
        redirect('admin/admin_home');
    }
} ?>

执行此函数后,我将重定向到以下其他方法。
<?php 
 public function unapproved_users() {        
    $result = $this->admin_notification_db->get_unapproved_users();
    $data['users'] = $result; 
    $this->load->view('admin/recent_users', $data); 
 } ?>

我检查了url_helper,在第540行有以下代码:case 'refresh' : header("Refresh:0;url=".$uri); - sangam
2个回答

8
Message: Cannot modify header information - headers already sent by (output started at    /home/samples1/public_html/venture-test/application/controllers/admin/admin_notifications.php:158)

在 PHP 代码中,如果在重定向头之前输出任何内容,则会出现错误。例如,请检查 PHP 标记之后或之前的任何空格,PHP 标记开始之前打印的 HTML 标记,或者在重定向函数之前输出或调试的任何内容。
如果您仍然遇到问题,请告知我更多信息。

谢谢,我找到了错误所在,我的 PHP 结束标签后面有一些空行,我把它们删除了,现在它可以正常工作了。 - sangam
抱歉,我忘记标记它了。 - sangam

-1

使用以下代码进行重定向。 echo 'window.location="'.base_url().'你的页面名称";';


不要这样做。它会破坏返回按钮的功能。 - Quentin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接