如何使用php和htaccess跟踪热连接器的ip地址

时间:2012-06-22 11:29:22

标签: php .htaccess

我正在尝试使用php和htaccess跟踪hotlinkers的IP地址。

.htaccess文件:

RewriteEngine on 
RewriteRule images/(.+)\.(jpg|gif|png) images.php 

images.php

?php 
    $ipAddress = $_SERVER['REMOTE_ADDR'];

    $statement=$db->prepare("INSERT INTO `ipaddress` (`ip` )
            VALUES (?)");
    $statement->execute(array($ipAddress));     

?>

现在用户请求像这样的图像www.domain.com/images/image.jpg它将重定向到images.php并跟踪他们的ips。我面临的问题是我的页面内部没有显示图像(原因是htaccess将其重定向到images.php)。我该如何解决这个问题?

Here is the link of previous question regarding this issue:

我不是htaccess的专家,所以需要更多解释

由于

1 个答案:

答案 0 :(得分:5)

如果您想记录IP然后再提供图像,那么这样的事情可能会:

.htaccess文件:

RewriteEngine on 
RewriteRule images/(.+)\.(jpg|gif|png) images.php?image=$1.$2

images.php:

<?php 
    $ipAddress = $_SERVER['REMOTE_ADDR'];

    $statement=$db->prepare("INSERT INTO `ipaddress` (`ip` )
            VALUES (?)");
    $statement->execute(array($ipAddress));

    $ext = strtolower(end(explode('.', $_GET['image'])));

    if($ext == 'gif') {
        $type = "gif";
    }
    else if($ext == 'jpg') {
        $type = "jpeg";
    }
    else if($text == 'png') {
        $type = "png";
    }
    else {
        $type = "binary";
    }

    header("Content-type: image/$type");
    readfile("images/" . $_GET['image']);

?>

您可能需要在此处调整路径以确保所有文件都在.htaccessimages.php中正确指向。