Welcome Guest, Not a member yet? Register   Sign In
404 page - more info, please
#7

[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>&lt;?php echo $heading; ?&gt;</h2>
    <p>&lt;?php echo $message; ?&gt;</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="&lt;?php echo base_url(); ?&gt;">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!


Messages In This Thread
404 page - more info, please - by El Forum - 01-30-2011, 05:07 AM
404 page - more info, please - by El Forum - 01-30-2011, 06:06 AM
404 page - more info, please - by El Forum - 01-30-2011, 06:13 AM
404 page - more info, please - by El Forum - 01-30-2011, 08:39 AM
404 page - more info, please - by El Forum - 01-30-2011, 04:18 PM
404 page - more info, please - by El Forum - 01-30-2011, 06:39 PM
404 page - more info, please - by El Forum - 01-30-2011, 06:55 PM
404 page - more info, please - by El Forum - 02-17-2011, 10:33 AM
404 page - more info, please - by El Forum - 02-17-2011, 02:27 PM
404 page - more info, please - by El Forum - 03-25-2011, 03:14 PM



Theme © iAndrew 2016 - Forum software by © MyBB