Welcome Guest, Not a member yet? Register   Sign In
A real custom 404 page
#1

[eluser]Unknown[/eluser]
Looking for a way to leave your code in tact without too much hassle to generate custom 404 pages. (Note in this example I use the template engine, my "content" area is defined as "content" in my template file. Make sure you match it accordingly)

Create a controller:
/application/controllers/error.php
Code:
<?php
class Error extends Controller {

    function Error()
    {
        parent::Controller();
    }

    function error_404()
    {
        $data['heading'] = urldecode($this->input->post('heading'));
        $data['message'] = urldecode($this->input->post('message'));
        $this->template->write('page_title', $data['heading']);
        $this->template->write('page_desc', $data['heading']);
        $this->template->write('page_keywords', $data['heading']);
        $this->template->write_view('content', 'error/error_404',$data);
        $this->template->render();
    }
}
/* End of file error_404.php */



Create a view (note I use the template engine, but I am sure you can make it work with your template engine)

/application/views/error/error_404.php

Code:
<h1>&lt;?php echo $heading; ?&gt;</h1>
&lt;?php echo $message; ?&gt;


EDIT
/application/errors/error_404.php

Code:
&lt;?php
if (function_exists('curl_init')) {
    $url = site_url("/error/error_404/");
    $fields = array(
        'heading'=>urlencode($heading),
        'message'=>urlencode($message)
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
    $content = curl_exec($ch);
    curl_close($ch);
    echo $content;
    exit;
} else {
    echo "<h1>".$heading."</h1>";
    echo $message;
    exit;
}
?&gt;


The file will now try to get the error page if CURL is installed, of not it will display the original message. If you have setup your template system well, it will render everything with the error messages in your content part.




Theme © iAndrew 2016 - Forum software by © MyBB