我正在尝试使用php和htaccess跟踪hotlinkers的ip地址.
.htaccess文件:
RewriteEngine on
RewriteRule images/(.+)\.(jpg|gif|png) images.php
Run Code Online (Sandbox Code Playgroud)
images.php
?php
$ipAddress = $_SERVER['REMOTE_ADDR'];
$statement=$db->prepare("INSERT INTO `ipaddress` (`ip` )
VALUES (?)");
$statement->execute(array($ipAddress));
?>
Run Code Online (Sandbox Code Playgroud)
现在,用户请求像这样的图像www.domain.com/images/image.jpg它将重定向到images.php并跟踪他们的ips.我面临的问题是我的页面内部没有显示图像(原因是htaccess将其重定向到images.php).我该如何解决这个问题?
我不是htaccess的专家所以需要更多的解释
谢谢
如果你想记录IP然后再提供图像,那么这样的事情可能会:
.htaccess文件:
RewriteEngine on
RewriteRule images/(.+)\.(jpg|gif|png) images.php?image=$1.$2
Run Code Online (Sandbox Code Playgroud)
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']);
?>
Run Code Online (Sandbox Code Playgroud)
您可能需要在此处调整路径以确保所有文件都正确指向,包括in .htaccess和in images.php.