Page 1 of 1

Download Tracking Question

Posted: Sat Jul 26, 2003 1:37 pm
by rprins
Ok, I have the following script in place on my site:

Code: Select all

<?php
include ("../includes/connect.php");

if(!empty($file)){
	$ref = $_SERVER["HTTP_REFERER"];
	$agent = $_SERVER["HTTP_USER_AGENT"];
	$remote = $_SERVER["REMOTE_ADDR"];
	$date = time();
	$query = "INSERT INTO file_tracker (file, RemoteAddr, agent, ref, date) VALUES ('$file', '$remote','$agent','$ref', '$date')";  
	$result = mysql_query($query) or die (mysql_error());
	header("Location:$file"); 
}
?>
What I am wondering is if there is a way to verify that the file has been downloaded before I add the information to the database. Currently it does it when the download dialogue comes up. So, this creates spam clicking and inflated download counts. Any help on how to get a "true" count of downloads of specific files? Thanks!

Posted: Sat Jul 26, 2003 2:24 pm
by PaTTeR
Make PHP script for serve files not just link to the file. Something like this:

Code: Select all

<a href="download.php">Download file</a>
Your php should sent specific headers and file content (ofcourse make record in DB before this). See http://php.net/manual/en/function.readfile.php - in user cntributed notes have a simple example.

Posted: Sat Jul 26, 2003 2:52 pm
by rprins
So, if I understand this correctly I would use readfile() to send the file to the user and when it returns TRUE I would post to the DB?

Posted: Sun Jul 27, 2003 2:17 am
by m3mn0n
yup...

Posted: Sun Jul 27, 2003 3:37 am
by patrikG
btw.: if you're using
rprins wrote:header("Location:$file");
to initiate the download: there is a known bug in some browsers.
For a solution look at this post.

Posted: Sun Jul 27, 2003 12:42 pm
by rprins
Ok, well I can't say that I have run into that header issue before, but I have been taking a look at that thread and others. I am still confused on how I go about and fix the following 2 issues:

1) Get the header() issue fixed for IE (I have read the post you suggested and I am soo confused)
2) Check to see if the file has been completely downloaded before posting the information to the DB. (I tried the readfile() as suggested above, but no luck... I might be doing it wrong).

And to PaTTeR, this is the beginning of a script to serve files for my site. So, it is just a work in progress. Thanks for all your help so far!

I still have the same code as my 1st post since I am confused on what to do next.