Free PHP Download Tracking Script
Database Table:
CREATE TABLE downloads (
file VARCHAR(60) NOT NULL,
counter INT UNSIGNED,
PRIMARY KEY (file)
);
|
Line By Line: dlt.php
- Create a string containing the header info we'll use to redirect the browser:
$cm = "Location: $file";
header ($cm);
- Connect to the DB
$db_user = "dbuser";
$db_passwd = "dbpwd";
$db_name = "dbname";
$db = mysql_connect("localhost", $db_user, $db_passwd);
mysql_select_db($db_name, $db);
- Update the downloads table - increment the counter for that page.
mysql_query("UPDATE downloads SET counter=counter+1 WHERE file='$file'",$db);
- If no row was affected by the last update statement, then there must have been no previous download of this file before, so we just need to add it to the table. We use an INSERT statement to insert the file name and '1' for the number of downloads.
if (mysql_affected_rows($db) < 1) {
$result = mysql_query("INSERT INTO downloads VALUES ('$file', 1)", $db); } }
- The final statement closes the connection to the database.
mysql_close($db); ?>
Usage...
- If you want to reset the counter, you just delete the appropriate rows from the database. So to delete the downloads for all files:
delete from downloads;
- To use the script you can have a download button by creating a form like this:
<form action="../dlt.php" method="post">
<input type="hidden" name="file" value="../ex/ebot.zip">
<input type="submit" value="Download">
</form>
- Or you can have a text link to the script by including the parameters in the URL:
<a href="Yourdomain.com/dlt.php?file=ebooks/MYPS!.zip">
|
|