Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can i count how many times an image is downloaded from my server?

Status
Not open for further replies.

pushyr

Programmer
Jul 2, 2007
159
GB
i've just read up on how to track opened emails and its a simple process of placing a 'web beacon' or a clear gif image into an html email. each time the email is opened the image is downloaded from the server and can be counted as an opened email. obviously info like ip address is collected so you can count uniques.

what i cant get my head around is how to count or gather the info needed for each time the image is requested from the server... using php. maybe im just not thinking clearly right now?? but hopefully someone can set me in the right direction??

much appreciated!!
 
two ways (of which there are many more)

call all your images something.php, have the php script update a database table each time it is called, then deliver the image using fpassthru or readfile or the like. you can have all images being called the same thing and differentiate with the query string.

method two: have a look at your weblogs.
 
hey jpadie, the first method sounds like something i would like to do, but can you elaborate a little more?
 
Code:
create table photos 
(id integer(10) primary key auto_increment,
path varchar(255) not null,
downloads integer(10) not null default 0);

Code:
$id = isset($_GET['id']) ? trim ($_GET['id']) : 0;
$result = mysql_query("select * from photos where id='".mysql_escape_string($id)."'";
$row = mysql_fetch_assoc($result);
if (is_file($row['path'])){
 header("Content-Type: image/jpg");
 readfile($row['path']);
 mysql_query("Update photos set downloads = downloads + 1 where id = id='".mysql_escape_string($id)."'";
 exit();
}
 
hi jpadie, sorry i didnt say thank you or provide any feedback. i haven't implemented your suggestions yet because i've realised that i'll first need to update to my email script.

which i also need help with...

i need to make the while loop that sends the bulk emails SLEEP for 1 second intervals between each email, then HOLD for 30 seconds between sending a batch of 50 emails.

here's the while loop that i'm using...

Code:
while($row_email = mysql_fetch_array($result)) {

loop through email addresses in table
plus insert emails into a different table for tracking
gather all variables... includes the web beacon that tracks the email... image.gif?id=emailaddress
send emails in the same loop

}
 
hi jpadie, i tried your code modifying it slightly...

Code:
$email_id = isset($_GET['email_id']) ? trim ($_GET['email_id']) : 0;

$image_path = '01_images/clear_1x1.gif';

if (is_file($image_path)){
 header("Content-Type: image/gif");
 readfile($image_path);
 mysql_query("UPDATE 14_tracker SET open = open + 1 WHERE email = email_id='".mysql_escape_string($email_id)."'");
 exit();
}


but i get this error...

Warning: Cannot modify header information - headers already sent by (output started at /my/web/directory/image.php:3) in /my/web/directory/image.php on line 64
GIF89a??‘?????ÿÿÿÿÿÿ???!ù???,???????L?;


im not sure what that means? to test the script i'm pasting the url and into the browser address bar to see what errors come up..


 
It usually means that you're outputting something to the browser prior to the header line (e.g. you've got an echo or print statement earlier in your script).

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
or even a single blank space or line break. typical errors are not opening the php tags as character 1 of a page.
 
ok i've sorted out the error and all is working, BUT a couple of issues remain...

1) the script increments twice rather than once. so if i open the email it updates twice.

so, im not sure why the script believes the image is being requested twice?

2) the script works when i open emails from a mac client or in yahoo web mail, etc., but does not work from outlook (outlook 2003 SP2).

i can see the issue is a missing image (only in outlook). do you think that outlook does not recognize image extensions such as image.php and it would only recongnize an extension such as gif, jpg, etc.?

here's my code...

Code:
<?php
$email_id = isset($_GET['email_id']) ? trim ($_GET['email_id']) : 0;

$image_path = '01_images/clear_1x1.gif';

if (is_file($image_path)){

	header( 'Content-type: image/gif' );
	readfile($image_path);
	Include("database.php");
	$query = "UPDATE 14_tracker SET open = open + 1 WHERE email = '$email_id'";
	mysql_query($query);
	$results = mysql_query ($query);
	
	
 exit();
}
?>
 
quite possibly, outlook might be trapping the bad image extensions. you could get around this by telling apache to parse jpg through php (.htaccess file)

as for the double counting - i suspect that a request is being sent once at preview and once when you actually open the file.

why are you trying to count how many times a particular email has been opened? or is the email id common to all the mass email you send in a batch (would be strange).

it also occurs to me that you should suppress errors (@) on the functions, so that your image does not appear broken if an error, for whatever reason, is thrown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top