• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Custom 404. Cannot modify header information.

#1
I'm using CodeIgniter 3.0.0 and trying to setup the custom 404 error pages. When I try to follow instructions online, the page works but I get a "Cannot modify header information" error that I can't figure out how to get rid of. How do I get rid of the error? I know that I can simply get rid of the
PHP Code:
$this->output->set_status_header('404'); 
line to remove the message but, best practices usually recommends that the page returns a 404 header at the same time. Also, all the examples that I've seen includes the line. Thank you in advance!

application\config\routes.php:
PHP Code:
$route['404_override'] = 'Errors/error_404_custom'

application\controllers\Errors.php:
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Errors extends CI_Controller
{
 
   public function __construct() 
 
   {
 
       parent::__construct(); 
 
   

 
   public function error_404_custom() 
 
   
        
$this->output->set_status_header('404'); 
        
$data['content'] = "error_404";
        
        
$this->load->view('header'$data);
        
$this->load->view('errors/error_404_custom'$data);
        
$this->load->view('footer'$data);
 
   

?>

application\views\errors\error_404_custom.php
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');
?>
<h1>404 Page Not Found.</h1>
The page you requested was not found. 

Error message:
Code:
A PHP Error was encountered
Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /application/controllers/Errors.php:33)

Filename: core/Common.php

Line Number: 564

Backtrace:

File: /application/controllers/Errors.php
Line: 25
Function: set_status_header

File: /index.php
Line: 292
Function: require_once

References:
Reply

#2
Your error message shows two different line numbers for /application/controllers/Errors.php and implies that $this->output->set_status_header('404') is on line 25. It's not clear what is being referred to as line 33 in the error message.
Reply

#3
Thank you for pointing that out. That's so weird. Line 33 refers to the closing php tag:
PHP Code:
?>

I deleted that line and the error goes away. So this is probably a silly question, but why would we not require that closing php tag?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.