获取文件查看统计信息

时间:2010-12-07 00:50:09

标签: php file statistics count

我有一个链接到其他子网站的文件。

http://site.com/file.img

其他网站将链接为

网站A:

<img src="http://site.com/file.img"></img>

网站B:

<img src="http://site.com/file.img"></img>

如何获取有关每个网站调用此文件的次数的统计信息? 这些结果会被篡改吗?例如网站A假网站B?

3 个答案:

答案 0 :(得分:3)

网站A确实可以伪造为B. HTTP Referer(原文如此)是假的。

你想要的是这样......

的.htaccess

RewriteEngine on

# Send all png, gif, jpeg, jpeg to image.php 
RewriteRule ^images/(.*)\.(png|gif|jpe?g)$ image.php?img=$1

PHP

<?php

if ( ! isset($_GET['img'])) {
    exit;
}

$img = $_GET['img'];

// Easily spoofed
if (isset($_SERVER['HTTP_REFERER'])
   AND $_SERVER['HTTP_REFERER'] === 'http://example.com') {
   // This image appears to be requested by example.com
}

// Avoid directory traversal attacks and validate image extension
if ( ! preg_match('/^[a-zA-Z0-9-_]+\.(gif|png|jpe?g)$/', $img)) {
   exit;
}

// Get image type
list(,,$type) = getimagesize($img);

// Send headers and echo file contents
header('Content-Type: ' . image_type_to_mime_type($type));
readfile($img);

如果您不想匹配此类特定的引荐来源字符串,可以使用parse_url()获取域名,然后进行比较(或者您想要比较)。

答案 1 :(得分:1)

简单的解决方案:使用URL重写将对文件的访问重定向到脚本(例如PHP),然后对它们进行计数,然后返回文件。

答案 2 :(得分:1)

如果您通过Referral变量进行检查,是的,它可以伪造。 但是如果运气好的话,A和B站点可以访问不同的IP地址 Web服务器日志和计数访问时间/ Ip addr