Help - Search - Members - Calendar
Full Version: Methods for conting downloads
MSFN Forums > Coding, Scripting and Servers > Web Development (HTML, Java, PHP, ASP, XML, etc.)
Pages: 1, 2

   
Google Internet Forums Unattended CD/DVD Guide
Xable
Well as title says, i have a free DLL download site and i want to count the number of downloads for each file. Is this possible?
Xable
dman, you know your stuff. is it possible...
liquidplasmaflow
You could use a system where the user clicks a PHP script to download, the script does counter++, and then the script sends the file.
dman
try this cgi script, caveman.
http://www.utilmind.com/scripts/downloadcounter.html. looks like you just need to alter your links a little to point to the script.

I like liquidplasmas idea for php.
call the script with a ?get parameter of the url of file from your table link.
then you could do a redirect something like this
CODE
<?php
// Redirect to the link URL
$url="files/mydll.dll";
Header("Location: $url")
?>

you can add some code to either increment a download count to a file or db. I wouldn't mind having this for my site as well, so I will try to help if you want to try it this way
Xable
carefull who you are calling caveman, i`ll hunt you down and eat you for dinner. biggrin.gif laugh.gif
that cgi script looks ok but liquidplasmaflow PHP script idea is interesting, even though i don`t understand PHP. I`m going to go through this tutorial tomorrow.
BTW is your site the one in your sig?
liquidplasmaflow
http://www.hotscripts.com/Detailed/33867.html <-- Try that. And to whose sig are you referring?
dman
Nice find, lpf. This is similar to what I was proposing, passing the download url to a php script as a get parameter.

"Neanderthal" (sorry, didn't realize cavemen, er, hominids were so touchy.) Go club some chick over the head and drag her back to your cave by her hair, that always makes you feel better! thumbup.gif
If you are referring to the link in MY sig, then no. I wish I was talented enough to write dabo. This project is created in Python by former Foxpro guru Ed Leafe. I learned a lot about Foxpro from him, but because MS is trying to kill it the foxpro community is moving on. I think I am doing well when I can figure out how to USE Ed's code.
this is My Site. Dont laugh, it is work in progress and I am no graphics artist. whistling.gif The freeware page and submission form are PHP/MySQL scripts. I am most proud of getting fixed menu frame to work in IE using CSS. Curse IE and its crappy CSS implementation, I only hope they can do better with IE7.

I am out of town till thursday. Read the tutes and if you still need help at end of week feel free to post or PM.
Xable
QUOTE (liquidplasmaflow @ Jul 18 2005, 03:19 AM)
http://www.hotscripts.com/Detailed/33867.html <-- Try that. And to whose sig are you referring?
*

You are the master script finder, thank you i will give it a go, dman refered to his site earlyer i was wondering if that was it in his sig.
QUOTE
"Neanderthal" (sorry, didn't realize cavemen, er, hominids were so touchy.) Go club some chick over the head and drag her back to your cave by her hair, that always makes you feel better! thumbup.gif

LOL
your site looks ok, though in the second paragraph "your" should be "you" tongue.gif
Thanks for your help thumbup.gif
dman
blushing.gif Thanks for the proofread. Swear I read it 100 times. Good luck, I am signing off til thurs eve. have fun!
dman
matrix0978
Wow, havent been in MSFN in a while. But I DID have a tutorial on this. I willl try and find it again and post it here.

EDIT:
I couldnt find my tutorial on it so i just did some research.

http://www.phpfreaks.com/quickcode/download_counter/230.php

http://www.phpjunkyard.com/php-click-counter.php
Xable
matrix0978, have you thought of writing that tutorial out again, it would be realy usefull for me, and others i think.

I`m trying http://www.hotscripts.com/Detailed/33867.html but can`t get it to work properly.
I can link to a file in the specified download folder but it won`t work if i link to a file that is in a folder inside the specified download folder.
Also i can`t figure out how to view the download statistics.
Any help appreciated. it`s got me wacko.gif
Xable
my download folder is called "dll" so i have set the variable in download.php like this,

CODE
$FILES_DIR = "/dll/";


and my links are therefore,

CODE
href='download.php?file=A3D.RAR'


but if i try to link to a file inside a sub folder inside my download folder. eg,#

CODE
/dll/A/A3D.RAR


the link

CODE
href='download.php?file=/dll/A/A3D.RAR'


or

CODE
href='download.php?file=/A/A3D.RAR'


won`t work.

Is there any way to make this work or can i only link to files in the root of my specified download folder?
dman
Hi B.C. laugh.gif
Instead of hard-coding the $FILES_DIR variable, pass it as another get parameter, like this..
CODE
href='download.php?file=A3D.RAR&filedir=A'

in the download script, set it like this...
CODE
$FILES_DIR = $_GET['filedir'];
Xable
dman, you are hilarious tongue.gif
thanx, doesn`t seem to work though.
dman
sorry, you need to still add the backslashes and the rest of your path to the string. Somrething like
CODE
$FILES_DIR = "/dll/" . $_GET['filedir'] . "/";
Xable
ye i go it working after a bit of a fiddle.
now do you know how i can view the statistics?
Realy i should go buy a book, i`m sure all this is basic stuff.

Thanks dman, where would i be without you biggrin.gif

Edit,
what do you think about this book Amazon

Edit 2,
Lookin at that code again.
QUOTE
sorry, you need to still add the backslashes and the rest of your path to the string. Somrething like
CODE
$FILES_DIR = "/dll/" . $_GET['filedir']. "/";

i still used
$FILES_DIR = $_GET['filedir'];

but changed
href='download.php?file=A3D.RAR&filedir=A'
to
href='download.php?file=A3D.RAR&filedir=dll/A/'
newwink.gif
dman
jolly good show! It doesn't matter where you add the path, as long as it's correct when the script tries to use it.
Really don't know about that book, sounds ok. I think you might be better off not using dreamweaver at first and just code in a PHP editor. This will give you better feel for the code IMHO. The PHP manual docs are really quite good, maybe look at them and search for tutes before buying anything, there is lots of free info out there.
As for stats, you need some SQL code. What info do you want to present, a list of downloaded files with count? I will put together an example.
Xable
QUOTE
jolly good show!

lol i don`t know anyone that speaks like that, i hear you though. Idealy, i would like to bin dreamweaver and just use notepad++. having said that though, building in DW and seing the results in code view helps a lot. and i do use notepad2 aswell.

Stats, don`t realy know what`s possible but i would like to see the download count for each file.
Cheers
dman
Of course you don't know anyone that speaks like that. I will rephrase "Ugh... Grunt MMMM" Better?

Try this for stats (change server, user, password & db name to suit)
Pretty it up as you would like.
CODE
<head>
    <title>File Download Statistics</title>
</head>
<body>
<?php
// Connecting, selecting database
$link = mysql_connect('localhost', 'root', '')
  or die('Could not connect: ' . mysql_error());

mysql_select_db('dlstats') or die('Could not select database');

// Performing SQL query
$query = 'SELECT file, count FROM dl_count order by file';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num=mysql_numrows($result);

// Printing results in HTML
$i=0;
echo "<table><tbody>\n";
echo "<tr><th>Download Count</th></tr>\n";
while ($i < $num) {
    $fname=htmlspecialchars(mysql_result($result,$i,"file"));
    $fcount=trim(mysql_result($result,$i,"count"));
    echo "\t<tr>\n";
    echo "\t\t<td>$fname</td>\n";
    echo "\t\t<td>$fcount</td>\n";
    echo "\t</tr>\n";
       $i++;
}
echo "</tbody></table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>
</body>
</html>
Xable
Typical.
Erm where do i put server, user, password & db name?
dman
server will be "localhost" unless you are making a direct connection (you're not).

username and password are whatever you set up. There will always be a username "root"

databasename is whatever you called the database that you created the dl_count table in.
CODE
$link = mysql_connect(*server*, *username*, *password*)
 or die('Could not connect: ' . mysql_error());

mysql_select_db(*databasename*) or die('Could not select database');
Xable
ok thought so, but how is it supposed to work. like i`ve saved it as .htm and .php both dont work.
dman
save as .php (like "showstats.php" or something)
what part is not working?
Xable
i saved as count.php uploaded to server and when i load it all that shows is this
CODE
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/.feliz/freedllboris/freedll.co.uk/count.php on line 7
Could not connect: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
dman
Your "download.php" script is working ok? and you are using the same user, pwd and db name in your count.php script?
Xable
fixed, changed localhost to the host name.
Check it out. Link

This is great but i have 2000 + files that will be monitored. Is there a more advanced way to manage the stats?

You are a massive help dman. Thank you. smile.gif
dman
thumbup.gif
What do you mean by "more advanced'. What info do you want to show. You are only tracking the file name and number of downloads, but this could be modified to include date, time, requestors IP, browser type... all kinds of things. What data do you want to track is the question?
You could also add filter to show only certain files or above certain level of downloads, or sort by the various fields of the db... you tell me.

You might also want to consider putting your list of files in a db and creating your page of downloads on the fly with PHP in the same way as the stats.
Xable
i mean like having the most downloaded file at the top going down in decending order and a total download count. just some basic stats like a summary or somthing rather than just reading down a list of 2000 + files.
QUOTE
You might also want to consider putting your list of files in a db and creating your page of downloads on the fly with PHP in the same way as the stats.

this sounds very interesting. do you mean like i put the file details in a db and then the pages are auto created from the info in the db.

I think that is what they do here. In their forum ppl request a file they can`t find and when the admin has found it he says, i have uploaded the file to the server and the website will be updated to show the file at 12:00a.m tonight.
just like, all he does is upload the file then everynight the website auotmaticly checks for new files and updates the pages.

I have thought about doing that in the past, but, as you know i am just learning the basics and thought it was best to start basic and work my way up. Is it hard though to set somthing like that up? I mean would it take long to learn. It would save a moutain of time in the long run.
dman
QUOTE
this sounds very interesting. do you mean like i put the file details in a db and then the pages are auto created from the info in the db.
Yes, that's exactly what I mean. This is how the freeware page on my site operates as well. I didn't get real fancy yet but notice that I have broken the programs into groups... that is one of many ways to slice the data. I am going to add a search and filter as well.
You are right, it saves a ton of edit time because you only need to change the code in one place inside the loop instead of editing each row by hand if you want to change or add something like the color or mouseover code.
The php code for the count page is a pretty generic skeleton for creating this type of page, just need to expand your database and SQL selects, and give it a form to submit parameters to the code. I will try to help as I work on my site. It is good that we are both working on similar project at the same time. Your questions are helping to provoke many of my own.
Xable
see, you have started simple then add features later, just like i planned, but, i think it might be best to convert my site to a dynamic one now rather than later.

QUOTE
just need to expand your database and SQL selects, and give it a form to submit parameters to the code
laugh.gif don`t understand any of that huh.gif
QUOTE
Your questions are helping to provoke many of my own.

and i bet i can`t answer any of em. laugh.gif
Here is a mother of a Q for you though,
How, exactly, do i create a datebase and have my pages dynamically constructed from it? tongue.gif
dman
Yes, you are better spending time up front with db rather than waste hours editing html pages.
Here is update for the stats to give you a little more example on things you can do. This includes total downloads and sorts by most downloads.
CODE
<head>
    <title>File Download Statistics</title>
</head>
<body>
<?php
// Connecting, selecting database
$link = mysql_connect('localhost', 'root', '')
  or die('Could not connect: ' . mysql_error());

mysql_select_db('dbname') or die('Could not select database');

// Performing SQL query for total downloads
$query = 'SELECT sum(count) as dlcount FROM dl_count';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "<b>Total Downloads " . mysql_result($result,0,"dlcount") . "</b>\n";
// Free resultset
mysql_free_result($result);

// Performing SQL query for individual downloads
$query = 'SELECT file, count FROM dl_count order by count desc';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num=mysql_numrows($result);

// Printing results in HTML
$i=0;
echo "<table><tbody>\n";
echo "<tr><th>Individual Downloads</th></tr>\n";
while ($i < $num) {
    $fname=htmlspecialchars(mysql_result($result,$i,"file"));
    $fcount=trim(mysql_result($result,$i,"count"));
    echo "\t<tr>\n";
    echo "\t\t<td>$fname</td>\n";
    echo "\t\t<td>$fcount</td>\n";
    echo "\t</tr>\n";
   $i++;
}
echo "</tbody></table>\n";
// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>
</body>
</html>

Next question, one step at a time. First decide what info about the files you would like to present or track, eg. name, category, size, publisher, etc. Then we can make the database to reflect this data. That needs to be done before you can move on to the PHP.
Xable
thats good, not sure what Individual Downloads are?

Name, Version and size thats all.

got to go now though, need some sleep. I`ll be on 2morow

seriously dman can`t thank you enough, your a star. thumbup.gif
dman
QUOTE
not sure what Individual Downloads are?

just something to call the list of files as opposed to the sum total. Call it anything you want.
get some sleep
Xable
lol i see now it`s game over see ya smile.gif
dman
Add this table to your database
CODE
CREATE TABLE 'dlls' (
'file' VARCHAR( 128 ) NOT NULL ,
'size' VARCHAR( 20 )  ,
'version' VARCHAR( 20 )
);

If you want to make the size and version mandatory add NOT NULL
If you have a list of your files attach it and I will write a little util to convert it into SQL INSERT statements to load up the database.
Xable
Fully rested now, biggrin.gif

1. What is a table? do the numbers define the widths of the cells?
CREATE TABLE 'dlls' (
'file' VARCHAR( 134 ) NOT NULL ,
'version' VARCHAR( 138 ) NOT NULL ,
'size' VARCHAR( 128 ) NOT NULL
);
2. What format does the list need to be in?
dman
a table is a database entity that is a group of related data items, like the file info all relates to the file. A database can have many tables, like yours will now have one for files to download, and one for download stats. Think of an HTML table with columns and rows... the columns are the data items you want to track, one row in the table for each record. hope this is clear, you could try reading mysql docs.
a table column can have different data types. varchar is for strings, that is most common. could also be number, logical, date, etc. Yes, the number is the max amount of characters that column will hold. my numbers were just guesses, make it appropriate for your data.

the list can be any text format, i will write util to read it. do you have a list, or still need to make one?
Xable
thats a little clearer thanx i will dive into the mysql docs when i get chance, i need to learn as much as i can.
So if i use
CREATE TABLE 'dlls' (
'file' VARCHAR( 134 ) NOT NULL ,
'version' VARCHAR( 138 ) NOT NULL ,
'size' VARCHAR( 128 ) NOT NULL
);
the table will be the same as this

Sorry i mean, like the format,
like this; name, version, size
AAAAMON.DLL 5.1.2600.0 25
Or
AAAAMON.DLL,5.1.2600.0,25
Or (the easyest to put together)
Name
Version
Size

A3D.DLL
2.9.0.0
96
dman
The comma seperated values (name, version, size) would be easiest, but the sequential row listing would work also. (I use visual foxpro for this kind of thing, it has super file and string handling functions)
Think maybe we should change "size" to numeric field so you can use it in stats for total bytes downloaded. The usual rule is to make numbers that will never need math done on them as varchar fields, like the version. Even though it is a number there is no math you can do on it. The size we can use to tally stats, so that can be numeric.
CODE
CREATE TABLE 'dlls' (
'file' VARCHAR( 134 ) NOT NULL ,
'version' VARCHAR( 138 ) NOT NULL ,
'size' INT( 11 ) NOT NULL
);


this will create numeric field of type "Integer" (whole numbers)

run "drop table 'dlls'; if you already created it, this will delete current structure
Xable
visual foxpro! can`t find download link. is it just for msdn subscribers.
i think it has to be the sequential row listing if you don`t mind, otherwise it`s a manual copy paste for each file.
i`ll pm you a link to the list in about 10min.
dman
You have to buy VFP. It is great language, but MS is trying to kill it in favor of .NET. They are letting it die slowly by not porting it to 64 bit, but it is still very useful.
If you already have the list in that format that is fine, I can read it.
Xable
sent check your pm
btw, i submitted the sql request it returnes this
QUOTE
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''dlls' (
'file' VARCHAR( 134 ) NOT NULL ,
'version' VARCHAR( 138 ) NOT NULL ,
' at line 1
dman
your list doesn't have version or size. need them too.
Xable
oops wrong one, try again. blushing.gif
Edit,
what about this

CREATE TABLE 'dlls' (
'file' VARCHAR( 134 ) NOT NULL ,
'version' VARCHAR( 138 ) NOT NULL ,
'size' INT( 11 ) NOT NULL
);

is it right?
dman
not sure why thats not working. might have something to do with the quotes.
try this (I tested this time). we are adding a primary key field, dont ask why yet, but we will use it to relate our different tables to one another.
CODE
CREATE TABLE `dlls` (
`pk` INT( 5 ) NOT NULL AUTO_INCREMENT ,
`file` VARCHAR( 134 ) NOT NULL ,
`version` VARCHAR( 138 ) NOT NULL ,
`size` INT( 11 ) NOT NULL ,
PRIMARY KEY ( `pk` )
)
dman
Here is link to sql insert file.

This is Foxpro code I used to convert, just in case you are interested...
CODE
fh = Fopen("c:\list.txt")
strout = ""
Do While !FEOF(fh)
  strtmp = "INSERT INTO dlls (file, size, version) VALUES ('"
  strtmp = strtmp + Fgets(fh) + "', '"
  strtmp = strtmp + Fgets(fh) + "', '"
  strtmp = strtmp + Fgets(fh) + "');" + Chr(13) + CHR(10)
  strout = strout + strtmp
Enddo
Fclose(fh)
Strtofile(ALLTRIM(strout),"c:\dllinsert.sql")
Xable
nice thanxs, i would love to get my hands on Foxpro and have a fiddle.
I hope you are going to let me know what i do with this file smile.gif unsure.gif
dman
what are you using to administer your db? I am using phpmyadmin. Just open the file and select all/copy and paste into sql window and run, same way as you are creating the tables. or use "browse" button and select the file and run.
Xable
Ok done, yes phpmyadmin aswell. had never seem phpmyadmin untill last week. It all looks very complicated.
dman
QUOTE
It all looks very complicated

Start Small... Evolve. It all becomes very simple, you just need to reach critical mass and it will all make sense.
Now look at the count.php page. Your list code will be very similar to this... open db, get recordset, loop through records creating a table row once per loop. that is basics, then we add the links for each letter, finally the paging code to show limited number of records per page...
Google Internet Forums Unattended CD/DVD Guide
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.