htaccess日志禁止访问

时间:2011-08-09 07:51:58

标签: .htaccess mod-rewrite

我的.htaccess中有几行阻止了图像的热链接。 这非常有效。然而我很好奇是要找出谁在做这件事。

当触发RewriteRule时,我是否有可能记录HTTP_REFERER?

由于

RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com/.*$ [NC] 
RewriteRule \.(gif|jpg|png)$ - [F,NC,L]

3 个答案:

答案 0 :(得分:2)

我会通过RewriteRule将热连接器重定向到给定的PHP站点,然后处理并保存IP地址($ _SERVER ['REMOTE_ADDR']),referer($ _SERVER ['HTTP_REFERER]等数据']),用户代理($ _SERVER ['HTTP_USER_AGENT'])和访问数据库的时间戳(例如MySQL)或单个文件。
但是你必须注意不要使用大量数据库/文件写入来重载服务器(如果有很多hotlinkers)。

我在下面测试了我的解决方案,它有效。

.htaccess档案:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com/.*$ [NC] 
RewriteRule .*\.(jpe?g|gif|bmp|png)$ log_hotlinkers.php [L]

log_hotlinkers.php档案:

<?php

    // I use PDO with a wrapper class
    require_once($_SERVER['DOCUMENT_ROOT'].'/PHP/classes/class.DB.php');

    $session_return_value = session_start();

    // is is already stored (per session)?
    $is_stored = (bool)!empty($_SESSION['user_visit_stored']);

    // we store this hotlinker only once per session
    if( !$is_stored ){

        // IP address
        $IP_address = $_SERVER['REMOTE_ADDR'];
        // user agent variable is not always set, in this case we set variable to "unknown"
        $HTTP_USER_AGENT = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'unknown';

        // session id
        $session_id = session_id();            

        // the $_SERVER['HTTP_REFERER'] variable is not always set (in this case we will store it with a NULL value)
        $HTTP_REFERER = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'NULL';



        // we store hotlinker's data in MySQL database (e.g. in a table called hotlinkers with an auto_increment id with the following fields)

        $query  = 'INSERT INTO hotlinkers ( IP_address, user_agent, session_id,  HTTP_REFERER ) ';
        $query .= '       VALUES ( :REMOTE_ADDR, :HTTP_USER_AGENT, :session_id, :HTTP_REFERER ) ;';

        // I use PDO with a wrapper class
        $stmt = DB::getDB()->prepare($query);
        $stmt->bindParam(':REMOTE_ADDR',     $IP_address );      // IP address
        $stmt->bindParam(':HTTP_USER_AGENT', $HTTP_USER_AGENT ); // user agent
        $stmt->bindParam(':session_id',      $session_id );      // session_id()
        $stmt->bindParam(':HTTP_REFERER',    $HTTP_REFERER );    // referer

        $exec_result = $stmt->execute();
        if( $exec_result ){
            // we store that agent is beállítjuk a megfelelő session-változót: a látogató eltárolva
            $_SESSION['user_visit_stored'] = true;
            $_SESSION['user_visit_time'] = time();
        }
        else{
            // throw new PDOException('Couldn\'t store hotlinker\'s data...');
        }   
    }

    // we output another image which signs blocking

    $path = './block-hotlinking-image.png';

    $im = imagecreatefrompng($path);

    header('Content-Type: image/png');

    imagepng($im);
    imagedestroy($im);

?>

在MySQL中,我的hotlinker表的结构如下:

CREATE TABLE `hotlinkers` (
   `id` int(11) not null auto_increment,
   `IP_address` varchar(40) not null default '0.0.0.0',
   `visit_timestamp` timestamp not null default CURRENT_TIMESTAMP,
   `user_agent` text not null,
   `session_id` char(32) not null,
   `HTTP_REFERER` varchar(255),
   PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

您可以在此处测试热链接Hotlink checker 我希望这很有帮助。

答案 1 :(得分:1)

所有禁止访问日志都已存在于您的access.log文件中。 它应该可以从您托管的客户面板访问。

如果您无法访问它们,唯一的解决方案是将这些请求重定向到php文件(或其他脚本),这将记录它们并将禁止的标头发送到浏览器。

答案 2 :(得分:1)

我不知道用.htacess命令记录某些内容的可能性。我认为最简单的方法是分析apache access.log文件。它应该包含REFERER,如果没有,请使用LogFormat directive

相关问题