72f4ba51b784673a15a1e89d8d9f49d1

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.

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 !

441c4f02db55ef2cbe96027af7012e01

techietim

December 12, 2007, December 12, 2007 23:40, permalink

No rating. Login to rate!
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);
    }
}
0481b9ea2b2aad6844b33dbca2d7fe0e

pkemper

December 13, 2007, December 13, 2007 07:19, permalink

1 rating. Login to rate!

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.

A2c8fecfd1fb707dd0a8f292ade77e1e

typefreak

December 14, 2007, December 14, 2007 19:12, permalink

1 rating. Login to rate!

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.

72f4ba51b784673a15a1e89d8d9f49d1

Martindale

December 14, 2007, December 14, 2007 19:17, permalink

No rating. Login to rate!

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.

5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

December 14, 2007, December 14, 2007 23:19, permalink

No rating. Login to rate!

You can use split() to split apart url and paramstring.

1
2
list($sUrl,$sParams)=split('?', $_SERVER['HTTP_REFERER']);
72f4ba51b784673a15a1e89d8d9f49d1

Martindale

December 17, 2007, December 17, 2007 20:32, permalink

No rating. Login to rate!

I don't understand how that applies to my script - is this generating an array of parameters?

6ba6351fbc3c11dca46407bb3f1e3c2d

troels

December 19, 2007, December 19, 2007 13:08, permalink

No rating. Login to rate!

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);
Bd200ce7a457b9dd91d18a9e6eff7d8f

daniel

March 17, 2008, March 17, 2008 03:36, permalink

No rating. Login to rate!

hello friend,
How are you doing pls can i get a php mailer link that is entring inbox?
Am waiting to hear from you soon.

from german daniel.

Your refactoring





Format Copy from initial code

or Cancel