Download Tracking Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
rprins
Forum Commoner
Posts: 31
Joined: Sat Jun 21, 2003 5:46 pm

Download Tracking Question

Post 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!
User avatar
PaTTeR
Forum Commoner
Posts: 56
Joined: Wed Jul 10, 2002 7:39 am
Location: Bulgaria
Contact:

Post 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.
User avatar
rprins
Forum Commoner
Posts: 31
Joined: Sat Jun 21, 2003 5:46 pm

Post 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?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

yup...
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
User avatar
rprins
Forum Commoner
Posts: 31
Joined: Sat Jun 21, 2003 5:46 pm

Post 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.
Post Reply