Welcome Guest, Not a member yet? Register   Sign In
Did we ever figure out custom 404 pages?
#2

[eluser]skunkbad[/eluser]
I've got a 404 page that works for me. It does send a 404 header, you can load whatever you want, it is called when show_404() is called, it does not redirect to another url, but it doesn't log the error (because I just don't care).

It doesn't take super genius php skills to achieve this. I just created a MY_Exceptions.php and use the following:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if (stristr($_SERVER['HTTP_HOST'], 'localhost' ))
{
    define("BASE", "http://localhost");
}
else
{
    define("BASE", "http://mysite.com");
}
class MY_Exceptions extends CI_Exceptions {

    function show_error($heading, $message, $template = 'error_general', $status_code = 500)
    {
        set_status_header($status_code);

        if (ob_get_level() > $this->ob_level + 1)
        {
            ob_end_flush();    
        }
        ob_start();
        // First context options are built for the development server, because it is a Windows server, and was not compiled --with-curlwrappers.
        if (stristr($_SERVER['HTTP_HOST'], 'localhost') || (substr($_SERVER['HTTP_HOST'], 0, 7) == '192.168')){
            $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);
        echo(file_get_contents( BASE .'/errorpage', FALSE , $context));
        $buffer = ob_get_contents();
        ob_end_clean();
        return $buffer;
    }

}
/* End of file MY_Exceptions.php */
/* Location: .application/libraries/MY_Exceptions.php */

Then, you can have an errorpage controller like this:

Code:
<?php
class Errorpage extends Controller {

    public function index()
    {
        print_r($_POST);
        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(
            'base_tag' => TRUE,
            'style_sheet_names' => array(
                                        'css/style.css'
            ),
            'media_targets' => array(
                                    'screen'
            ),
            'slogan' => 'Maybe your fingers are too fast for you?',
            'content' => $this->load->view('errorpage', '', TRUE ),
            'dynamic_extras' => $this->load->view('countdown', '', TRUE)
        );
        $this->load->view('template_content', $data );
    }
}
/* End of file errorpage.php */
/* Location: .application/controllers/errorpage.php */

You may have to tweak the stream context depending on your server...


Messages In This Thread
Did we ever figure out custom 404 pages? - by El Forum - 04-23-2010, 11:40 PM
Did we ever figure out custom 404 pages? - by El Forum - 04-24-2010, 01:26 AM
Did we ever figure out custom 404 pages? - by El Forum - 04-24-2010, 08:43 AM
Did we ever figure out custom 404 pages? - by El Forum - 04-24-2010, 10:34 AM
Did we ever figure out custom 404 pages? - by El Forum - 04-24-2010, 10:44 AM
Did we ever figure out custom 404 pages? - by El Forum - 04-24-2010, 03:39 PM
Did we ever figure out custom 404 pages? - by El Forum - 04-24-2010, 03:48 PM
Did we ever figure out custom 404 pages? - by El Forum - 04-24-2010, 06:27 PM
Did we ever figure out custom 404 pages? - by El Forum - 04-24-2010, 07:18 PM
Did we ever figure out custom 404 pages? - by El Forum - 04-24-2010, 09:15 PM
Did we ever figure out custom 404 pages? - by El Forum - 04-24-2010, 11:31 PM
Did we ever figure out custom 404 pages? - by El Forum - 04-25-2010, 12:00 AM
Did we ever figure out custom 404 pages? - by El Forum - 05-18-2010, 04:59 AM
Did we ever figure out custom 404 pages? - by El Forum - 05-18-2010, 08:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB