1 2 3 4 5 6 7 8 9 10 11 12
<?php if (substr($_SERVER['HTTP_REFERER'], 0, 33) == "http://www.thegrandtournament.com") { } else { $myFile = "log.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "\n".$_SERVER['HTTP_REFERER']; fwrite($fh, $stringData); fclose($fh); } ?>
Refactorings
No refactoring yet !
techietim
December 12, 2007, December 12, 2007 23:40, permalink
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php $domains = array(); $ref = $_SERVER['HTTP_REFERER']; if (substr($ref, 0, 33) != "http://www.thegrandtournament.com"){ $myFile = "log.txt"; $parsed = parse_url($ref); if(!in_array($ref, array_map("trim", file($myFile))) && !in_array($parsed['host'], $domains)){ $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = $ref."\n"; fwrite($fh, $stringData); fclose($fh); } }
pkemper
December 13, 2007, December 13, 2007 07:19, permalink
Why not run a cron job (say weekly) that parses through your web server request log to parse out the requests? Also, you might think of storing the data in a database, which is surely faster when the list gets long.
typefreak
December 14, 2007, December 14, 2007 19:12, permalink
Keep in mind that the 'http_referer' can't be fully trusted, as it is send by the client.
If you follow the above suggestion (about the db), make sure you escape the information properly.
Martindale
December 14, 2007, December 14, 2007 19:17, permalink
Is there any way I can clean up a number of GET parameters, for example any and all SID= fields? This is causing a lot of duplicates in the flat-file storage.
Eineki
December 14, 2007, December 14, 2007 23:19, permalink
You can use split() to split apart url and paramstring.
1 2
list($sUrl,$sParams)=split('?', $_SERVER['HTTP_REFERER']);
Martindale
December 17, 2007, December 17, 2007 20:32, permalink
I don't understand how that applies to my script - is this generating an array of parameters?
troels
December 19, 2007, December 19, 2007 13:08, permalink
You can use parse_str to parse the query string in the URL. And it's easier to use error_log for appending to a text-file, than fopen.
You should follow pkemper's advice and clean the data in a separate process.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if (strpos($_SERVER['HTTP_REFERER'], '?') !== false) {
list($base, $query_string) = split('?', $_SERVER['HTTP_REFERER']);
parse_str($query_string, $parameters);
$tmp = array();
foreach ($parameters as $key => $value) {
if (!in_array($parameters, array('SID'))) {
$tmp[] = rawurlencode($key) .'='. rawurlencode($value);
}
}
$referer = $base . '?' . implode('&', $tmp);
} else {
$referer = $_SERVER['HTTP_REFERER'];
}
error_log($referer, 3, $myFile);
This code is embedded within an image script - the image can be loaded dynamically on any page. I want to keep track of what pages have displayed the image, without duplicates (the script below does not check for duplicates), and excluding a list of domains. This is hacked to kingdom come, and simply writes the referer to the end of a file. When the file gets large, the image slows down.