[eluser]skunkbad[/eluser]
This is the way I've been handling errors. Works for more than just 404:
MY_Exceptions:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if (stristr($_SERVER['HTTP_HOST'], 'localhost' ))
{
define('BASE', 'http://' . DEVELOPMENT_SERVER_HOSTNAME);
}
else
{
define('BASE', 'http://' . PRODUCTION_SERVER_HOSTNAME);
}
class MY_Exceptions extends CI_Exceptions {
function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
set_status_header($status_code);
// First context options are built for the xampp development server. It was not compiled --with-curlwrappers.
if (BASE == 'http://' . DEVELOPMENT_SERVER_HOSTNAME)
{
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=> "Content-type: application/x-www-form-urlencoded\r\n" ,
'user_agent'=> $_SERVER['HTTP_USER_AGENT'],
'content' => http_build_query( array ( 'heading' => $heading,
'message' => $message,
'status_code' => $status_code
),
'',
'&'
)
)
);
// or else if the script is running from the production server, the options are built as needed (with the header options as an array)
}
else
{
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=> array(
'Content-type: application/x-www-form-urlencoded'
),
'user_agent'=> $_SERVER['HTTP_USER_AGENT'],
'content' => http_build_query( array ( 'heading' => $heading,
'message' => $message,
'status_code' => $status_code
)
)
)
);
}
$context = stream_context_create($opts);
return file_get_contents( BASE .'/errorpage', FALSE , $context);
}
}
errorpage controller:
Code:
<?php
class Errorpage extends MY_Controller {
public function index()
{
if($this->input->post('heading'))
{
$vars['title'] = $this->input->post('heading');
$vars['heading'] = $this->input->post('heading');
}
else
{
$vars['title'] = '404 - PAGE NOT FOUND';
$vars['heading'] = '404 - PAGE NOT FOUND';
}
if($this->input->post('message'))
{
$vars['message'] = $this->input->post('message');
}
else
{
$vars['message'] = 'The page you tried to access does not exist or has been moved.';
}
if($this->input->post('status_code'))
{
$vars['status_code'] = $this->input->post('status_code');
}
else
{
$vars['status_code'] = '404';
}
$this->load->vars($vars);
// insert page specific data into template
$data = array(
'content' => $this->load->view('errorpage', '', TRUE ),
'dynamic_extras' => $this->load->view('countdown', '', TRUE)
);
$this->load->view('template_content', $data );
}
}
errorpage view:
Code:
<div id="single-column">
<h2><?php echo $heading; ?></h2>
<p><?php echo $message; ?></p>
<p>You will be redirected back to the home page in <span id="countdown" style="color:red; font-weight:bold;">15</span> seconds<br />unless you have disabled javascript.</p>
<p>Click <a href="<?php echo base_url(); ?>">HERE</a> if you don't want to wait.</p>
</div>
I'd love to know if there is a better/easier way to handle
ALL errors!