-
number of downloads
I have a page with a few different files that can be downloaded. I'd like to keep track of how many times those things are being downloaded.
I've been thinking this over and I'm guessing the only way to do this would be to make the download link open up a page that inserts a value into a DB field, correct?
Is there any way to do this without opening a new page?
-
here's some rough theory:
you're going to need one of those "your download will start in 5 seconds, or click here" pages.. or, well, you could make the link be like "download.php?id=4354" and then when you go to that link, don't send anything to the browser, but query the database instead, and send a location header to the file.. it should work.
something like this:
PHP Code:
<?
/*******************************
* download.php by david miles *
*******************************/
//get the file id
if(!empty($_GET['id']) && is_numeric($_GET['id'])){
$fileid = $_GET['id'];
}else{
$fileid = 0;
}
if(checkID($fileid)){
//id is valid
//get the file's name from the database
$q = mysql_query("SELECT filename FROM table WHERE id='$fileid'");
$q = mysql_fetch_array($q);
$file = "http://www.site.com/files/" . $q['filename'];
if(file_exists($file)){
//update the download count
updateDL($fileid);
//redirect the user
header("Location: $file");
}else{
error(1);
}
}else{
//id isn't valid
error(0);
}
function updateDL($id){
//update the database
$q = "UPDATE table SET count=count+1 WHERE id='$id'";
mysql_query($q);
}
function error($id){
switch($id){
case 0;
echo "Invalid file ID!";
break;
case 1;
echo "File does not exist!";
break;
}
exit;
}
?>
might have some syntax errors and such, built that off of memory and is untested.. but i guess it points you in a general direction, so i hope it helps.
-
Quote:
Originally posted by kows
something like this:
PHP Code:
<?php
if(checkID($fileid)){
//...
}
?>
You didn't define a checkID() function, unless it already exists within PHP and I don't know about it?
-
crap, I must've forgotten that.. that was a busy day. I actually have the function, I meant to grab it from something I made before and add it in, but anyway, it's something like this:
PHP Code:
function checkID($id){
$q = mysql_query("SELECT id FROM table WHERE id='$id';");
$q = mysql_fetch_array($q);
if($q[0] > 0){
return $q[0];
}
}
anyways, sorry about that
-
Quote:
Originally posted by kows
crap, I must've forgotten that.. that was a busy day. I actually have the function, I meant to grab it from something I made before and add it in, but anyway, it's something like this:
PHP Code:
function checkID($id){
$q = mysql_query("SELECT id FROM table WHERE id='$id';");
$q = mysql_fetch_array($q);
if($q[0] > 0){
return $q[0];
}
}
anyways, sorry about that
If you ask me, kows, that's overkill right there. I don't have time to type up my alternative, but you basically have the same query here:
Code:
$q = mysql_query("SELECT filename FROM table WHERE id='$fileid'");
In the if statement. If the query returns no results, you know the ID was invalid.
I just don't think you should run two database queries when you can get away with one.
Just my 7 cents.