Codeigniter - Weird parameter 'favicon.ico'

2019-08-30 10:59发布

问题:

I am working with Codeigniter PHP framework. I have the typicall Pages controller for displaying pages. I also added code for storing in the session variable in which page was the user before login. The thing is I'am getting as parameter 'favicon.ico' which make no sense because the page that was called was 'login' and displays well after that.

First as a temporary solution I checked the $page variable for the 'favicon.ico', and the weird thing was that after checking it I had inside the $page variable 'login'. It was very weird, I had to make a second check for login page as you see here for making it work. :

    public function view($page = 'index')
{
    if($page != 'favicon.ico'){
        if($page != "login"){
            $this->session->set_userdata('actual_page', $page);
        }               
    }

    if ( ! file_exists('application/views/pages/'.$page.'.php'))
    {
        // Whoops, we don't have a page for that!
        $page="error";
    }
    // guardamos la página actual en la sesión


    $data['title'] = ucfirst($page); // Capitalize the first letter

    $this->load->view('templates/header', $data);
    $this->load->view('pages/'.$page, $data);       
    $this->load->view('templates/footer', $data);

}

My question is, what is happening? Why if I only check for $page == 'login' it doesn't work. This is crazy.

回答1:

The browser sends a request to your web server

GET /favicon.ico HTTP/1.1

in order to retrieve Favicon. You can change the location of favicon with a link tag as described in the wikipedia article.

Try moving the file_exists check before you set the session data? The web application will need to be able to handle missing resources and return 404 accordingly.



回答2:

Old question, but I think I have a better answer...

  1. Assuming your favicon.ico is in the root directory, make sure your CodeIgniter .htaccess file includes it in the RewriteCond so it isn't passed to the CodeIgniter index.php:

    RewriteCond $1 !^(assets|images|robots.txt|favicon\ .ico)

  2. In your html files, include the full path to your favicon.ico, for example using CodeIgniter's base_url() function:

    < link rel="shortcut icon" href="< ?php echo base_url('favicon.ico'); ?>" />

By including the full path to the icon, it won't get mysteriously passed to your controllers' _remap() function with "favicon.ico" in the $params array.