开发者

Codeigniter 404 errors - How to tell where they're coming from?

For example in my logs I have many lines that repeat, such as:

ERROR - 2011-07-06 09:19:01 --> 404 Page Not Found --> favicon.ico

Is there any way for me to find out who is calling these errant URLs? The favicon is just an example, there are some URLs that for example show an intent to hack, and others it's just the same error repeated over and over and over again. Basically I'd love to know which IP's to potentially block as well as what sites to contact if they have bad links (or if I should just redir开发者_如何学编程ect them on my server with .htaccess).


If you feel the need to do this, just extend the Exceptions class and override the show_404() function:

// v2.x: core/MY_Exceptions.php
// v1.x: libraries/MY_Exceptions.php
class MY_Exceptions extends CI_Exceptions {

    /**
     * 404 Page Not Found Handler
     *
     * @access  private
     * @param   string
     * @return  string
     */
    function show_404($page = '', $log_error = TRUE)
    {
        $heading = "404 Page Not Found";
        $message = "The page you requested was not found.";

        // By default we log this, but allow a dev to skip it
        if ($log_error)
        {
            // Custom code here, maybe logging some $_SERVER variables 
            // $_SERVER['HTTP_REFERER'] or $_SERVER['REMOTE_ADDR'] perhaps                 
            // Just add whatever you want to the log message

            log_message('error', '404 Page Not Found --> '.$page);
        }

        echo $this->show_error($heading, $message, 'error_404', 404);
        exit;
    }
}

If you keep getting stuff like favicon.ico, there's a good chance this is your fault, so you might want to look into that.

Just to clarify:

  • $_SERVER['HTTP_REFERER'] will give you the URL the page was requested from, so you can see where the request originated, whether it be on your site or elsewhere.
  • $_SERVER['REMOTE_ADDR'] should give you the IP address the request was made by

http://php.net/manual/en/reserved.variables.server.php

Brief Example:

    if ($log_error)
    {
        $msg = '';
        if (isset($_SERVER['HTTP_REFERER']))
        {
            $msg .= 'Referer was '.$_SERVER['HTTP_REFERER'];            
        }
        else
        {
            $msg .= 'Referer was not set or empty';  
        }
        if (isset($_SERVER['REMOTE_ADDR']))
        {
            $msg .= 'IP address was '.$_SERVER['REMOTE_ADDR'];           
        }
        else
        {
            $msg .= 'Unable to track IP';  
        }
        log_message('error', '404 Page Not Found --> '.$page.' - '.$msg);
    }


I believe if you want this, you'd have to log it yourself by creating a custom 404 page and manually logging to the log file. You can access the IP address in PHP via $_SERVER['REMOTE_ADDR'].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜