Getting file view statistics
I have a file that is being linked to from other sub websites.
http://site.com/file.img
开发者_运维知识库The other websites would link to it as
website A:
<img src="http://site.com/file.img"></img>
website B:
<img src="http://site.com/file.img"></img>
How can I get statistics about the number of times each website called this file? Could these results be tampered? For example could website A fake being website B?
Website A could indeed fake being B. The HTTP Referer (sic) is trivial to fake.
What you want is something like this...
.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);
If you don't want to match such a specific referrer string, you can use parse_url()
to get the domain, and then compare that (or however you want to compare).
Simple solution: use URL Rewriting to redirect accesses to your file to a script (e.g. a PHP) counting them and then returning the file.
If you are checking by Referral variable, yes it can be faked. But if you have luck that sites A and B are on different IP addresses you can access web server logs and count access times / Ip addr
精彩评论