2012-06-22 36 views
3

我试图跟踪使用PHP和HTTAccess hotlinkers的IP地址。我如何跟踪热链接器IP地址使用PHP和htaccess

.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会重定向到图像的图像。 PHP和跟踪他们的ips。我在这里面临的问题是我的页面内没有显示图像(原因htaccess重定向到images.php)。我该如何解决这个问题?

Here is the link of previous question regarding this issue:

我不是htaccess的专家如果要记录的IP,然后服务形象,无论如何,那么这样的事情可能这样做需要更多的解释

感谢

+0

你想做什么?你想记录IP和显示图像,或只是登录并退出给每个人,只显示图像,而无需登录自己的网站? –

+0

我想记录IP并显示图像。 –

回答

5

.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

+0

$ ext = lower(end(explode('。',$ filename))); 这里是$ filename和lower()?用strtolower()? –

+0

@Compiler哎呀,抱歉,复制/粘贴问题。我更新了我的答案。 –

+0

我正在尝试images/image.png,但其他所有时间部分正在执行。我想在$ _GET ['image']出错。在htaccess中,什么是$ 1. $ 2? –