Results 1 to 39 of 39

Thread: Downloads counter

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    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.

  2. #2
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  3. #3
    scoutt
    Guest
    I've done this and you are pretty close Matt. all it is is like a regular counter.

  4. #4

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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.

  5. #5
    scoutt
    Guest
    it all depends on if you use mysql or a flat file

  6. #6

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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.

  7. #7
    scoutt
    Guest
    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>

  8. #8

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Talking 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.

  9. #9

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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=\&quot;3;URL=$url/&quot;>";

    ?>

  10. #10

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    oops. I see one error. $query and $sql. I guess I should make up my mind.

  11. #11
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  12. #12

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    same error.

  13. #13

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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.

  14. #14
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  15. #15

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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.

  16. #16
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  17. #17
    scoutt
    Guest
    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...

  18. #18

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Thumbs up

    Woo Hoo! It works! Thanks Matt.

    Have I mentioned that I think PHP is awesome??

  19. #19

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Will this work with relative paths?

    example:

    temp.php?file=..\downloads\myfile.zip

  20. #20

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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?

  21. #21
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  22. #22

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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"); 

  23. #23
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  24. #24
    scoutt
    Guest
    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.

  25. #25

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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?

  26. #26
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  27. #27
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  28. #28

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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?

  29. #29
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Not that I know of.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  30. #30

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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?

  31. #31
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  32. #32

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    what's the html code for "Back?" I don't know what page they'll be coming from.

  33. #33
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    <a href="javascript:history.go(-1);">Go Back</a>
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  34. #34
    scoutt
    Guest
    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.

  35. #35

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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.

  36. #36
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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!
    My evil laugh has a squeak in it.

    kristopherwilson.com

  37. #37

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    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.

  38. #38
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  39. #39
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    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.


    Things I do when I am bored: DotNetable

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width