|
-
Jun 10th, 2002, 04:27 PM
#1
Thread Starter
PowerPoster
Downloads counter
I'm trying to figure out how to determine how many times a file has been downloaded from my site. The site statistics themselves are pretty vague and don't go down to the individual file level.
So how can I do it with PHP or otherwise? All I want to know is how many times a specific file has been downloaded.
Thanks.
-
Jun 10th, 2002, 05:16 PM
#2
Fanatic Member
This is a vague "guess" because I have not attempted this yet, but I believe this is the method of doing such a thing.
1) You need a script that will have an id for each file you so wish to download or a script that will receive a variable telling it which file the user wants to download.
Example of Link to Script: http://www.whatever.com/download.php?file=start.zip
2) When download.php is loaded you would check to see if start.zip is inside a table and if it is you increment its count field by one. If it is not, then you add it to the database and set its count to 1.
That is how I see it being done, if you have any questions dont hesitate to ask.
-Matt
-
Jun 10th, 2002, 05:50 PM
#3
I've done this and you are pretty close Matt. all it is is like a regular counter.
-
Jun 10th, 2002, 06:44 PM
#4
Thread Starter
PowerPoster
Can someone post a code snippet please? Basically, they will click the link to the file, which will no longer be a link to the file, right? It will be a link to a php page that updates the counter and then starts the download if I'm getting this right.
My PHP knowledge is the smallest number that's greater than 0, so use small words please.
-
Jun 10th, 2002, 07:58 PM
#5
it all depends on if you use mysql or a flat file
-
Jun 10th, 2002, 08:02 PM
#6
Thread Starter
PowerPoster
Originally posted by scoutt
it all depends on if you use mysql or a flat file
I can do either. Personally, I just want to go the simplest route. MySQL will probably be the easiest since I don't know how to access a flat file in a secure folder.
-
Jun 10th, 2002, 08:17 PM
#7
ok chill out as this is not that hard. you will need to store the files name in the database and then do something like this. you need to make a seperate file and when you link to a file they can download you make it go to this file.
PHP Code:
$download_loc = "path/to/".$_REQUEST['sfile'];
sfile = "the name of the file they clicked on"
$sql = mysql_query("SELECT * FROM $table WHERE sfile='".$_REQUEST['sfile']."'");
while ($row = mysql_fetch_array($sql)) {
$count=$row["siteclick"];
$id = $row["id"];
}
$sql2 = mysql_query("UPDATE $table SET siteclick='$count + 1' WHERE sfile='".$_REQUEST['sfile']." ' ");
echo"<META HTTP-EQUIV=\"Refresh\" Content=\"3;URL=$download_loc\">";
so your link will look like this
<a href="download.php?sfile=$name_of_file">File name.zip</a>
-
Jun 10th, 2002, 08:26 PM
#8
Thread Starter
PowerPoster
Just call me "Frosty"
OK, I looked it over and see more or less what's happening. I gotta get some food and then I'll come back and see what I can do. Thanks for the help.
-
Jun 10th, 2002, 09:25 PM
#9
Thread Starter
PowerPoster
I set up the database like this:
database name = softwaredownload
table name = files
fields:
id = Autoincrement; Primary index
url = absolute path to file
downloadcount = number of times file has been downloaded
size = file size in KB
I have two pages temp.php and temp1.php. temp.php has the link and temp1.php handles the details. I'm getting an error that the query didn't return any results. I added one file to the database and it's id = 1.
http://www.paulkjohnson.com/software/ezdb/temp.php
temp.php
Code:
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Master Source Code</title>
<meta name="Microsoft Theme" content="glacier 011, default">
</head>
<body leftmargin="060" background="_themes/glacier/glabkgnd.jpg" bgcolor="#FFFFFF" text="#000000" link="#006666" vlink="#999999" alink="#66CCCC"><!--mstheme--><font face="Arial, Arial, Helvetica">
<p><a href="temp1.php?FILE_ID=1">Master Source Code</a></p>
<!--mstheme--></font></body>
</html>
temp1.php
PHP Code:
<?PHP
$db = @mysql_connect("localhost:/home/username/paulkjohnson.com/.database/mysql/mysql.sock", "root", "password") or die("Could not establish connection with database");
mysql_select_db('softwaredownload') or die(mysql_error());
$sql = mysql_query("SELECT * FROM $files WHERE id='".$_REQUEST['FILE_ID']."'");
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {
$count=$row["downloadcount"];
$id = $row["id"];
$url = $row["url"];
}
$sql2 = mysql_query("UPDATE $files SET downloadcount='$count + 1' WHERE id='".$_REQUEST['FILE_ID']." ' ");
echo"<META HTTP-EQUIV=\"Refresh\" Content=\"3;URL=$url/">";
?>
-
Jun 10th, 2002, 09:26 PM
#10
Thread Starter
PowerPoster
oops. I see one error. $query and $sql. I guess I should make up my mind.
-
Jun 10th, 2002, 09:28 PM
#11
Fanatic Member
I dont want to sound as if I am whining but I dont like that way. Why tell the database what files to expect? Why not just search the database for the file they clicked on if its found update its count column if its not found add it to the database?
This way you do not have to tell the database before hand what files to expect.
Last edited by cpradio; Jun 10th, 2002 at 09:31 PM.
-
Jun 10th, 2002, 09:29 PM
#12
Thread Starter
PowerPoster
same error.
-
Jun 10th, 2002, 09:32 PM
#13
Thread Starter
PowerPoster
Originally posted by cpradio
I dont want to sound as if I am whining but I dont like that way. Why tell the database what files to expect? Why not just search the database for the file they clicked on if its found update its count column if its not found add it to the database?
This was you do not have to tell the database before hand what files to expect.
I think that's what I'm doing, except I'm sending an ID. From the ID, the database gives the url and the download should start. All I care about is having a counter. I don't care how it happens. The less complicated the better. But every time I work with PHP I spend hours (or days) trying to get one or two lines of code to work.
-
Jun 10th, 2002, 09:36 PM
#14
Fanatic Member
I totally understand that you do not want it to get complicated but it just seems to me (from my point of view) that having to type in all the files you want to have a counter on into the database would be a huge waste of time, versus having them automatically added if they do not already exist.
I know it kinda sound self-absorb to some extent and however you wish to do it is cool by me, I just wanted to know why scoutt would choose that route versus the one I am thinking of. Maybe there is a logical explanation for it and maybe there isnt, but none the less it just seems weird.
-Matt
-
Jun 10th, 2002, 09:41 PM
#15
Thread Starter
PowerPoster
OK, back to square 1. Can somebody please post some code that will store a counter in a textfile or a database or whatever for how many times a file has been downloaded from my site? Or send me a link that has a code snippet. I don't know jack about PHP, but my server supports it. What that means is that I can't fill in the blanks. I don't have a clue about what the single quotes, double quotes and dots are used for and the more I read, the more confused I get. All I want is an answer please.
Thanks.
-
Jun 10th, 2002, 09:52 PM
#16
Fanatic Member
Here is how I would do it:
Sample Link: http://www.whatever.com/download.php?file=tutorial1.zip
PHP Code:
$db = @mysql_connect("localhost", "username", "password") or die("Could not establish connection with database");
mysql_select_db('files') or die(mysql_error());
$query = mysql_query("select * from downloads where file='$file'");
$number = mysql_numrows($query);
if ($number != 0)
$query = mysql_query("update downloads set count = count +1 where file='$file'");
else
$query = mysql_query("insert into downloads (file,count) values ('$file','1')");
echo"<META HTTP-EQUIV=\"Refresh\" Content=\"3;URL=$file\">";
Now to use this all you need is one database called files and then make a table inside it called downloads (both all lowercase)
change "username" to the username you use to login into your phpMyAdmin or Control Panel and do the same with password.
When you make the downloads table, you need 3 fields:
id int(11) not null auto increment primary
file varchar(255) not null
count int(11) not null
the first value refers to the name of the field, the second value refers to the type, the third refers whether the null or not null drop down should be choosen, the auto increment is located in the extra menu, and primary means to check the primary checkbox.
-Matt
-
Jun 10th, 2002, 10:04 PM
#17
I choose that way because I use something similar to that. I also have a form that I can upload files so I ain't typing in the files all the time. also it lets users upload files as well.
that way I have files that are in the DB and I am not the only one that can add them.
if you want to do it another way be my guest, won't hurt my feelings, I will just go away and cry...
-
Jun 10th, 2002, 10:05 PM
#18
Thread Starter
PowerPoster
Woo Hoo! It works! Thanks Matt. 
Have I mentioned that I think PHP is awesome??
-
Jun 10th, 2002, 10:07 PM
#19
Thread Starter
PowerPoster
Will this work with relative paths?
example:
temp.php?file=..\downloads\myfile.zip
-
Jun 10th, 2002, 10:09 PM
#20
Thread Starter
PowerPoster
OK, I just answered my own question. Relative paths work. Now how do I redirect them back to the original page they started the download from?
-
Jun 10th, 2002, 10:10 PM
#21
Fanatic Member
Originally posted by scoutt
I choose that way because I use something similar to that. I also have a form that I can upload files so I ain't typing in the files all the time. also it lets users upload files as well.
that way I have files that are in the DB and I am not the only one that can add them.
if you want to do it another way be my guest, won't hurt my feelings, I will just go away and cry...
I was just wondering why you choose that way. The same goes for mine, you can make a form to upload files and they will be added to the database when a user attempts to download any of them for the first time. I just saw it kind of pointless to use so much code for downloading a file.
Like I said in a different thread, everyone has their own techniques and that is why makes each of us programmers different from the next. Without that difference there wouldnt be any use in forums such as these to share our ideas on how to go about certain scripts. Fact of the matter is that all that really counts is the outcome and if both scripts both produce the outcome expected then the goal has been met.
-Matt
-
Jun 10th, 2002, 10:14 PM
#22
Thread Starter
PowerPoster
OK, for some reason this skips the download and sends the user straight back to the download page. Why doesn't it start the download first? Do I need some kind of wait statement or something?
PHP Code:
echo"<META HTTP-EQUIV=\"Refresh\" Content=\"3;URL=$file\">";
header ("Location: downloads.htm");
-
Jun 10th, 2002, 10:14 PM
#23
Fanatic Member
copy the meta tag line of code, change the 3 to a 10 and the URL to the path you want them to go to after the download starts.
-Matt
-
Jun 10th, 2002, 10:16 PM
#24
boy, that almost looks like the same code I did, except for the insert if it is not there.
well the way I did it I don't have to put the link on my site if it is not in the database. all my pages are dynamic and I don't write any html. all the info is from the DB. if a file is in the DB then they can download it, if not then they can't.
-
Jun 10th, 2002, 10:19 PM
#25
Thread Starter
PowerPoster
Originally posted by cpradio
copy the meta tag line of code, change the 3 to a 10 and the URL to the path you want them to go to after the download starts.
-Matt
Now it gets it sits at the php page (blank) doesn't download and doesn't redirect. Let's say I wanted to go back to any page that was sent. How would I send the page I'm on as a variable to the php page so that I always go back to where ever I came from?
-
Jun 10th, 2002, 10:19 PM
#26
Fanatic Member
Originally posted by cafeenman
OK, for some reason this skips the download and sends the user straight back to the download page. Why doesn't it start the download first? Do I need some kind of wait statement or something?
PHP Code:
echo"<META HTTP-EQUIV=\"Refresh\" Content=\"3;URL=$file\">";
header ("Location: downloads.htm");
You cannot use header as it is compiled before echo thus the page being relocated before the download begins.
-
Jun 10th, 2002, 10:22 PM
#27
Fanatic Member
Originally posted by scoutt
boy, that almost looks like the same code I did, except for the insert if it is not there.
well the way I did it I don't have to put the link on my site if it is not in the database. all my pages are dynamic and I don't write any html. all the info is from the DB. if a file is in the DB then they can download it, if not then they can't.
Which is understandable. I figured he was going to read the directory and list the files that way, that way its still dynamic.
Either way the outcome is the same and the files are counted based on download. Two great minds, but two very different thoughts.
-Matt
-
Jun 10th, 2002, 10:22 PM
#28
Thread Starter
PowerPoster
I changed it like you said. Changed 3 to 10 and changed the url
So now I have the following two lines:
PHP Code:
echo"<META HTTP-EQUIV=\"Refresh\" Content=\"3;URL=$file\">";
echo"<META HTTP-EQUIV=\"Refresh\" Content=\"10;URL=http://www.paulkjohnson.com/software/ezdb/downloads.htm">";
Also, if the user cancels the download, I still get a count for it. Is there a way to tell if the user actually downloaded the file?
-
Jun 10th, 2002, 10:27 PM
#29
Fanatic Member
-
Jun 10th, 2002, 10:33 PM
#30
Thread Starter
PowerPoster
OK, well the download starts, but it takes about 5 seconds and there's no indication to the user about what's going on.
So I have that problem and the redirection problem still. It will not redirect back to any page I specify regardless of how I've coded it. I'm trying absolute url's just to get it working, but it doesn't work for some reason. In other words, the second line isn't working. Any ideas?
-
Jun 10th, 2002, 10:37 PM
#31
Fanatic Member
Write text on there, design the download page with html under the php coding. Provide a link back to the download area that is clickable.
-Matt
-
Jun 10th, 2002, 10:38 PM
#32
Thread Starter
PowerPoster
what's the html code for "Back?" I don't know what page they'll be coming from.
-
Jun 10th, 2002, 10:45 PM
#33
Fanatic Member
<a href="javascript:history.go(-1);">Go Back</a>
-
Jun 10th, 2002, 10:50 PM
#34
why have a download page? if you click on the link and it should automatically download. 2 of the same meta tags will not work together.
if you want a download page then you have to make them go there and tehn have them click the link. but if you do that then what is the point of having a counter if they can bypass it by just enter the path in the browser? I would leave it like the first way you had it, that way you get the exact count instead of them bypassing it.
also if they click cancel after they clicked on the link, then they should be downloading it in the first place.
-
Jun 10th, 2002, 10:55 PM
#35
Thread Starter
PowerPoster
You lost me. Ever since I started doing this, I've been sending the filename to another page that does all the database stuff and starts the download.
-
Jun 10th, 2002, 10:59 PM
#36
Stuck in the 80s
I personally like this: "http://www.site.com/download.php?id=41" better than: "http://www.site.com/download.php?file=file.zip" as a way of seeing lnks.
I do agree that it is much easier to do it this way in the code. I would probably do it with the ID anyways and predefine the files, but I would have an upload.php to add them like scoutt said. I can see both arguments very well, and it's just a matter of opinions. You are both teh genyuses!
-
Jun 10th, 2002, 11:04 PM
#37
Thread Starter
PowerPoster
I agree with you on that one hobo. I don't have a ton of files to download and when I add a new one, it won't be a big deal to add it to the database myself. I'm just glad it's working for now. But I'll probably change it to send an ID instead of a filename.
-
Jun 11th, 2002, 05:36 AM
#38
Fanatic Member
Originally posted by scoutt
ok chill out as this is not that hard. you will need to store the files name in the database and then do something like this. you need to make a seperate file and when you link to a file they can download you make it go to this file.
PHP Code:
$download_loc = "path/to/".$_REQUEST['sfile'];
sfile = "the name of the file they clicked on"
$sql = mysql_query("SELECT * FROM $table WHERE sfile='".$_REQUEST['sfile']."'");
while ($row = mysql_fetch_array($sql)) {
$count=$row["siteclick"];
$id = $row["id"];
}
$sql2 = mysql_query("UPDATE $table SET siteclick='$count + 1' WHERE sfile='".$_REQUEST['sfile']." ' ");
echo"<META HTTP-EQUIV=\"Refresh\" Content=\"3;URL=$download_loc\">";
so your link will look like this
<a href="download.php?sfile=$name_of_file">File name.zip</a>
PHP Code:
$download_loc = "path/to/".$_REQUEST['sfile'];
sfile = "the name of the file they clicked on"
$sql = mysql_query("SELECT * FROM $table WHERE sfile='".$_REQUEST['sfile']."'");
$sql2 = mysql_query("UPDATE $table SET siteclick=siteclick + 1 WHERE sfile='".$_REQUEST['sfile']." ' ");
echo"<META HTTP-EQUIV=\"Refresh\" Content=\"3;URL=$download_loc\">";
I modified scoutt's version just a tda. Now you can get rid of that pesky while loop. The only thing you really need now is an upload script.
-Matt
-
Jan 23rd, 2004, 07:05 AM
#39
Fanatic Member
This is going to be one of my first projects so I found this really useful.
One thing I am not sure on yet is whether to use flat files or mySQL. It will probably be mySQL as it will be easier to maintain and easier admin.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|