Return to Snippet

Revision: 66971
at July 26, 2014 07:24 by stiva71


Updated Code
create a /track and a /track/log directory under webroot

find a 1x1 transparent gif image and upload to the /track directory as t.gif

add .htaccess file to the track directory

-.htaccess--------

<filesMatch "\.(gif|txt)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>

IndexIgnore *

RewriteEngine on
RewriteRule .(gif)$ display.php [NC]

-end .htaccess---------


Create display.php in the /track directory

---display.php-----

<?php
//ini_set('display_startup_errors',1);
//ini_set('display_errors',1);
//error_reporting(-1);

function writeAccess($filename) {

  //time for log
  if( ($time = $_SERVER['REQUEST_TIME']) == '') {
    $time = time();
  }


if (empty($_SERVER['REMOTE_ADDR'])){
  $remote_addr = ' unknown';
      $remoteHost = ' unknown';
}   else {

      $remote_addr = $_SERVER['REMOTE_ADDR'];
  $remoteHost= gethostbyaddr($remote_addr);
  //$remoteHost='no';
  }


//$_SERVER["HTTP_USER_AGENT"]
if (empty($_SERVER['HTTP_USER_AGENT'])){
    $remoteAgent=' No User Agent';
} else  {
  $remoteAgent=$_SERVER['HTTP_USER_AGENT'];

  }

   // Format the date and time
  $date = date("Y-m-d H:i:s", $time);

//remove extension then spaces
$filename = preg_replace("/\\.[^.\\s]{3,4}$/", "", $filename);//remove 3or4 char extension
$filename = str_replace(' ', '', $filename); //remove spaces
$filename = $_SERVER["DOCUMENT_ROOT"] . '/track/log/'. $filename .'.txt';

 // Append to the log file
  if($fd = @fopen($filename, "a")) {
    $result = fputcsv($fd, array($date, $remote_addr, $remoteHost, $remoteAgent));
    fclose($fd);
      }
 }

//get name of request file filter out any spaces and %20 and sanitize
$url_array = explode("/", $_SERVER["REQUEST_URI"]);
$filename = $url_array[ count($url_array) - 1 ];
$filename=str_replace('%20', '-', $filename);
$filename=htmlspecialchars(trim($filename));

//log
writeAccess($filename);


//$data = file_get_contents($_SERVER["DOCUMENT_ROOT"] . '/track/t.gif');


header("Content-Type: image/gif");
//echo $data;
readfile($_SERVER["DOCUMENT_ROOT"] . '/track/t.gif');
exit();


---------end display.php------



Add <img src="http://website.co.uk/track/loggingname.gif"> to the email, this must not be embedded ie in thunderbird mail moz-do-not-send attribute must be set true.

any requests for this gif image will be redirected through display.php which will log the request and serve up the stored t.gif image.

the log file name will be as the requested gif file in this case loggingname.txt in the /log directory

Revision: 66970
at July 26, 2014 07:19 by stiva71


Initial Code
create a track directory under webroot

find a 1x1 transparent gif image and upload to the directory as t.gif

add .htaccess file

-.htaccess--------

<filesMatch "\.(gif|txt)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>

IndexIgnore *

RewriteEngine on
RewriteRule .(gif)$ display.php [NC]

-end .htaccess---------


Create display.php

---display.php-----

<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

function writeAccess($filename) {

  //time for log
  if( ($time = $_SERVER['REQUEST_TIME']) == '') {
    $time = time();
  }


if (empty($_SERVER['REMOTE_ADDR'])){
  $remote_addr = ' unknown';
      $remoteHost = ' unknown';
}   else {

      $remote_addr = $_SERVER['REMOTE_ADDR'];
  $remoteHost= gethostbyaddr($remote_addr);
  //$remoteHost='no';
  }


//$_SERVER["HTTP_USER_AGENT"]
if (empty($_SERVER['HTTP_USER_AGENT'])){
    $remoteAgent=' No User Agent';
} else  {
  $remoteAgent=$_SERVER['HTTP_USER_AGENT'];

  }

   // Format the date and time
  $date = date("Y-m-d H:i:s", $time);

//remove extension then spaces
$filename = preg_replace("/\\.[^.\\s]{3,4}$/", "", $filename);//remove 3or4 char extension
$filename = str_replace(' ', '', $filename); //remove spaces
$filename = $_SERVER["DOCUMENT_ROOT"] . '/track/log/'. $filename .'.txt';

 // Append to the log file
  if($fd = @fopen($filename, "a")) {
    $result = fputcsv($fd, array($date, $remote_addr, $remoteHost, $remoteAgent));
    fclose($fd);
      }
 }

//get name of request file filter out any spaces and %20 and sanitize
$url_array = explode("/", $_SERVER["REQUEST_URI"]);
$filename = $url_array[ count($url_array) - 1 ];
$filename=str_replace('%20', '-', $filename);
$filename=htmlspecialchars(trim($filename));

//log
writeAccess($filename);


//$data = file_get_contents($_SERVER["DOCUMENT_ROOT"] . '/track/t.gif');


header("Content-Type: image/gif");
//echo $data;
readfile($_SERVER["DOCUMENT_ROOT"] . '/track/t.gif');
exit();


---------end display.php------

Initial URL


Initial Description
Add an image to the email, must be linked not embedded ie in thunderbird mail moz-do-not-send attribute must be set true.

any requests for this gif image will be redirected through display which will log the request and serve up the stored t.gif image.

the log file name will be as the requested gif file in this case loggingname.txt in the /log directory

Initial Title
email tracking image logging

Initial Tags
email, php, image

Initial Language
PHP