在htaccess重定向后从图像中获取奇怪的字符

时间:2021-09-09 00:24:35

I want to get the HTTP_USER_AGENT of the viewer after viewing the image by a script.

我希望在通过脚本查看图像后获取查看器的HTTP_USER_AGENT。

Here is the directory

这是目录

/www
|-- /other.org
|-- /example.com
    |-- /sub
        |-- .htaccess
        |-- agentlog.php
        |-- /i
            |-- photo.png

I have separated the image files and the script to different folders. The root of all the files is in the subdirectory of the domain. So http://example.com/sub, the images are stored at http://example.com/sub/i. The .htaccess file will redirect all images to a PHP file which gets the HTTP_USER_AGENT and stores it in a database and also display the image through a subdomain which will not be redirected by the .htaccess.

我已将图像文件和脚本分离到不同的文件夹。所有文件的根目录都在域的子目录中。所以http://example.com/sub,图片存储在http://example.com/sub/i。 .htaccess文件将所有图像重定向到PHP文件,该文件获取HTTP_USER_AGENT并将其存储在数据库中,并通过子域显示图像,该子域不会被.htaccess重定向。

In more detail, the user types in http://example.com/sub/photo.png to the browser, to the viewer it will just look like that's the image and the viewer will not notice anything else or any redirection. However, what happens when the user types in is that

更详细地说,用户在浏览器中输入http://example.com/sub/photo.png,对于查看者来说,它看起来就像是图像,观众不会注意到任何其他内容或任何重定向。但是,当用户输入时会发生什么

The .htaccess will check if photo.png exists in http://example.com/sub/i/photo.png, and redirect the request to a file named agentlog.php at http://example.com/sub/agentlog.php if the image exists and then agentlog.php will log and save to the database and show the photo.png through http://i.example.com/photo.png where http://i.example.com points to /www/sub/i.

.htaccess将检查http://example.com/sub/i/photo.png中是否存在photo.png,并将请求重定向到http://example.com/sub/agentlog上名为agentlog.php的文件.php如果图像存在,然后agentlog.php将记录并保存到数据库并通过http://i.example.com/photo.png显示photo.png,其中http://i.example.com指向/网络/子/ I。

Here is the .htaccess file

这是.htaccess文件

RewriteEngine On
RewriteCond %{HTTP_HOST} !^i\. [NC]
RewriteCond %{DOCUMENT_ROOT}/i/$1 -f
RewriteRule ^(.+\.(?:jpe?g|png|gif))$ agentlog.php [NC,L]

Here is the agentlog.php file without the sql code

这是没有sql代码的agentlog.php文件

<?php
$path = $_SERVER['REQUEST_URI'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$ext = pathinfo($_SERVER['REQUEST_URI'], PATHINFO_EXTENSION);
$filename = basename($path);

header("Content-Type: image/$ext");

$img = "http://i.example.com/$filename";
readfile($img);

Everything works well except the image doesn't actually show itself, the .htaccess redirects. It shows

一切都很好,除了图像实际上没有显示自己,.htaccess重定向。表明

�PNG  IHDR�^���� �IDATx��ݯ��u���............

and not the actual image itself.

而不是实际的图像本身。

If I directly access the image, it works fine so I think the problem lies in the .htaccess but I do not know what other rule to write that can make this work.

如果我直接访问图像,它工作正常所以我认为问题出在.htaccess但我不知道还有什么其他规则可以使这个工作。

If another further information is needed I will be glad to add in. How do I solve this? Or are there any methods to go about doing this? I have already put in the Content Header but not sure why it doesn't work.

如果需要其他更多信息,我将很乐意加入。我该如何解决这个问题?或者有什么方法可以做到这一点?我已经放入了内容标题,但不确定它为什么不起作用。

2 个解决方案

#1


2  

If you are using closing tag ?>check, if there is any white-space or newline character afterwards. Omitting closing tag is considered as a good practice.

如果您正在使用结束标记?>检查,之后是否有任何空格或换行符。省略结束标记被认为是一种很好的做法。

Edits

Try this:-

        $img = "http://i.example.com/$filename";
        header("Content-Type: image/$ext");
        header('Content-Length: ' . filesize($img)); 
        ob_clean(); // Clean (erase) the output buffer.
        flush(); // Flush system output buffer.
        readfile($img); // Output the file to browser.
        exit; // Stop execution of the script

#2


0  

Everything works well except the image doesn't actually show itself, the .htaccess redirects. It shows

一切都很好,除了图像实际上没有显示自己,.htaccess重定向。表明

�PNG IHDR�^���� �IDATx��ݯ��u���............

PNGIHDR ^ IDATx ݯ u ............

It's a header's error (mime type error) - text/plain instead of image/...

这是标题的错误(mime类型错误) - text / plain而不是image / ...

Anyway your .htaccess does not work in my environment It should be something like this:

无论如何你的.htaccess在我的环境中不起作用它应该是这样的:

RewriteEngine On

RewriteCond %{REQUEST_URI} (.+\.(?:jpe?g|png|gif))
RewriteRule ^(.+\.(?:jpe?g|png|gif))$ agentlog.php [NC,L]

And PHP code can be something like this

PHP代码可以是这样的

<?php
$path = $_SERVER['REQUEST_URI'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$ext = pathinfo($_SERVER['REQUEST_URI'], PATHINFO_EXTENSION);
$filename = basename($path);

header("Content-Type: image/".$ext);

readfile($filename);
?>

#1


2  

If you are using closing tag ?>check, if there is any white-space or newline character afterwards. Omitting closing tag is considered as a good practice.

如果您正在使用结束标记?>检查,之后是否有任何空格或换行符。省略结束标记被认为是一种很好的做法。

Edits

Try this:-

        $img = "http://i.example.com/$filename";
        header("Content-Type: image/$ext");
        header('Content-Length: ' . filesize($img)); 
        ob_clean(); // Clean (erase) the output buffer.
        flush(); // Flush system output buffer.
        readfile($img); // Output the file to browser.
        exit; // Stop execution of the script

#2


0  

Everything works well except the image doesn't actually show itself, the .htaccess redirects. It shows

一切都很好,除了图像实际上没有显示自己,.htaccess重定向。表明

�PNG IHDR�^���� �IDATx��ݯ��u���............

PNGIHDR ^ IDATx ݯ u ............

It's a header's error (mime type error) - text/plain instead of image/...

这是标题的错误(mime类型错误) - text / plain而不是image / ...

Anyway your .htaccess does not work in my environment It should be something like this:

无论如何你的.htaccess在我的环境中不起作用它应该是这样的:

RewriteEngine On

RewriteCond %{REQUEST_URI} (.+\.(?:jpe?g|png|gif))
RewriteRule ^(.+\.(?:jpe?g|png|gif))$ agentlog.php [NC,L]

And PHP code can be something like this

PHP代码可以是这样的

<?php
$path = $_SERVER['REQUEST_URI'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$ext = pathinfo($_SERVER['REQUEST_URI'], PATHINFO_EXTENSION);
$filename = basename($path);

header("Content-Type: image/".$ext);

readfile($filename);
?>