Posted By

stiva71 on 07/26/14


Tagged

email php image tracking


Versions (?)

email tracking image logging


 / Published in: PHP
 

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

  1. create a /track and a /track/log directory under webroot
  2.  
  3. find a 1x1 transparent gif image and upload to the /track directory as t.gif
  4.  
  5. add .htaccess file to the track directory
  6.  
  7. -.htaccess--------
  8.  
  9. <filesMatch "\.(gif|txt)$">
  10. FileETag None
  11. <ifModule mod_headers.c>
  12. Header unset ETag
  13. Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
  14. Header set Pragma "no-cache"
  15. Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  16. </ifModule>
  17. </filesMatch>
  18.  
  19. IndexIgnore *
  20.  
  21. RewriteEngine on
  22. RewriteRule .(gif)$ display.php [NC]
  23.  
  24. -end .htaccess---------
  25.  
  26.  
  27. Create display.php in the /track directory
  28.  
  29. ---display.php-----
  30.  
  31. <?php
  32. //ini_set('display_startup_errors',1);
  33. //ini_set('display_errors',1);
  34. //error_reporting(-1);
  35.  
  36. function writeAccess($filename) {
  37.  
  38. //time for log
  39. if( ($time = $_SERVER['REQUEST_TIME']) == '') {
  40. $time = time();
  41. }
  42.  
  43.  
  44. if (empty($_SERVER['REMOTE_ADDR'])){
  45. $remote_addr = ' unknown';
  46. $remoteHost = ' unknown';
  47. } else {
  48.  
  49. $remote_addr = $_SERVER['REMOTE_ADDR'];
  50. $remoteHost= gethostbyaddr($remote_addr);
  51. //$remoteHost='no';
  52. }
  53.  
  54.  
  55. //$_SERVER["HTTP_USER_AGENT"]
  56. if (empty($_SERVER['HTTP_USER_AGENT'])){
  57. $remoteAgent=' No User Agent';
  58. } else {
  59. $remoteAgent=$_SERVER['HTTP_USER_AGENT'];
  60.  
  61. }
  62.  
  63. // Format the date and time
  64. $date = date("Y-m-d H:i:s", $time);
  65.  
  66. //remove extension then spaces
  67. $filename = preg_replace("/\\.[^.\\s]{3,4}$/", "", $filename);//remove 3or4 char extension
  68. $filename = str_replace(' ', '', $filename); //remove spaces
  69. $filename = $_SERVER["DOCUMENT_ROOT"] . '/track/log/'. $filename .'.txt';
  70.  
  71. // Append to the log file
  72. if($fd = @fopen($filename, "a")) {
  73. $result = fputcsv($fd, array($date, $remote_addr, $remoteHost, $remoteAgent));
  74. fclose($fd);
  75. }
  76. }
  77.  
  78. //get name of request file filter out any spaces and %20 and sanitize
  79. $url_array = explode("/", $_SERVER["REQUEST_URI"]);
  80. $filename = $url_array[ count($url_array) - 1 ];
  81. $filename=str_replace('%20', '-', $filename);
  82. $filename=htmlspecialchars(trim($filename));
  83.  
  84. //log
  85. writeAccess($filename);
  86.  
  87.  
  88. //$data = file_get_contents($_SERVER["DOCUMENT_ROOT"] . '/track/t.gif');
  89.  
  90.  
  91. header("Content-Type: image/gif");
  92. //echo $data;
  93. readfile($_SERVER["DOCUMENT_ROOT"] . '/track/t.gif');
  94. exit();
  95.  
  96.  
  97. ---------end display.php------
  98.  
  99.  
  100.  
  101. 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.
  102.  
  103. 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.
  104.  
  105. the log file name will be as the requested gif file in this case loggingname.txt in the /log directory

Report this snippet  

You need to login to post a comment.