i want a script that counts and lists how many times a file has been downloaded
or how many times a link has been clicked
and i want it to post the number on the page for visitors to see
can anyone provide that?
Printable View
i want a script that counts and lists how many times a file has been downloaded
or how many times a link has been clicked
and i want it to post the number on the page for visitors to see
can anyone provide that?
1 - Put the following code into a file called "counter.php";2 - Create the text file defined in the $counter value/location above and just put a 0 (zero) in it. The sample code file is called "counter.txt".PHP Code:<?php
$counter = 'path/to/counter.txt'; // text file to store download count - create manually and put a 0 (zero) in it to begin the count
$download = 'http://mywebsite.com/file/to/download.zip'; // the link to your download file
$number = file_get_contents($counter); // read count file
$number++; // increment count by 1
$fh = fopen($counter, 'w'); // open count file for writing
fwrite($fh, $number); // write new count to count file
fclose($fh); // close count file
header("Location: $download"); // get download
?>
3 - Create a download link to the "counter.php" file instead of the actual download file;4 - To display the download count on your web page;Code:<a href="path/to/counter.php">DOWNLOAD</a>
Hope that helpsPHP Code:<?php echo file_get_contents('path/to/counter.txt');?>
thank you
is it possible to do this with multiple links on a single page?
kind of like this site:
http://www.sxc.hu/category/9001
see under each picture it mentions how many times it has been downloaded
Yes, just setup a php and txt file per download.
counter1.php, counter2.php, counter3.php, etc
counter1.txt, counter2.txt, counter3.txt, etc
If you want to track lots of downloads though, you'd be better off using a download/click tracker with a database: http://www.hotscripts.com/category/s...lick-tracking/
yes what i need is one with a database! since i have hundreds of links and it would take too much time to make counter files for each link
i checked out ur link....the only problem is that the scripts given there are all for the webmaster to see...i didnt really understand how i can show the download times on my page instead of just seeing it on a control panel.
is that possible with those?
Sorry, I have no idea as I've never needed to set one up for public display. You would need to look through a few scripts from the listing - maybe try/setup a few demos and read their documentation to find one that suits your needs - I would expect that some have that facility through referencing a database id, but each script is going to be different. You could also try contacting a few developers via their websites or if they have their own forums, approach them there.
yes i really liked this one that you provided in the external link :
http://www.scottconnell.com/php/click_tracker/
but unfortunately i couldnt figure how to show the number like he does at the end of that page
and he hasnt placed his contacts so i can ask him!!
bad luck i guess!
What did you need help with? Contact me here http://www.scottconnell.com/send/mail/
Scott,
I sent you an email asking about your script but didnt get reply
Are you sure you mean bbcode? Bbcode is a special markup used to format text in forum posts.
More info here http://en.m.wikipedia.org/wiki/BBCode
Please provide more information about how/where you'd be using a flat file counter script and maybe we can assist.
Hi Bev, would you clarify something for me?
I've got this script working fine as you can see here;
http://www.castlefordheritagetrust.org.uk/zSandPit.php
However what is the purpose of this line;
$download = 'http://mywebsite.com/file/to/download.zip'; // the link to your download file
I've taken it out and it still works, the php is
<?php
$counter = 'php/counter.txt'; // text file to store download count - create manually and put a 0 (zero) in it to begin the count
$number = file_get_contents($counter); // read count file
$number++; // increment count by 1
$fh = fopen($counter, 'w'); // open count file for writing
fwrite($fh, $number); // write new count to count file
fclose($fh); // close count file
header("Location: $download"); // get download
?>
Cheers
John
$download is the variable which stores the downloadable file (ie the zip file in my example), so that at the end of the script, after all the count logging is done, the file is downloaded. The header() functions in the last line summons the download.
In my earlier example I have pointed a download link to a php file that contains this code instead of directly to the downloadable file. An alternative use as you have discovered, is that you can instead paste the code directly into a web page and use it as a hit counter (with the $download line and header line both removed as they would be redundant)
Excellent, thanks for that.
All I need now is the Trust newsletter :)
Just wondering if this page was still alive? I have a similar need for the code please check:
http://wavapps.net/bin/bin.htm ... I have had some luck with the second one but with a different code. What would work for me in these pages without changing them to .php instead of htm? I would like for the number of downloads to display above the picture where it says Downloads. http://wavapps.net/wismec.htm
Thanks in advance...
I do not understand with it all broke up like that. I need to see altogether. but I am studying it. Thank you though;-)
For reference, here is the updated complete PHP code plus download pack. http://www.dynamicdrive.com/forums/e...)-AJAX-amp-PHP
However, this will not work for a .htm/.html page unless your server is setup to parse PHP in HTML pages.
You couldn't really provide a counter on the client-side in JavaScript alone because the counts could only be saved on each viewing computer (using cookies or localStorage) - they'd be different for each computer looking at the page. You would need a server-side language such as PHP in order to stores counts on the server and make them accessible by anyone viewing the web page.
I think that is a little closer to what i am looking to do. It seems that you are showing me a way to do all the counting of lets say the 146 logos with the use of only 3 files. A lot to digest and I think I am starting to get it...
Thanks a great deal
Here,
Thank You, Beverleyh.
Step 1,2,3 worked for me but Step no.4 didn't gave me result.
Because i was having my page where result was to shown was html page not php page.
So I want the code of Step 4 in html format!
Can this be done? If possible how?
Waiting for reply, Thank You again...
You could use an iframe or AJAX. Using iframe is easiest. Make up a php file called display_counter.php and put the php code on it:
Then on your HTML page have an iframe:Code:<?php echo file_get_contents('path/to/counter.txt');?>
You could even skip the step of the intermediate php page and just do:Code:<iframe name="counter" src="display_counter.php" width="90" height="30" scrolling="no" frameborder="0"></iframe>
But sometimes displaying a text file in an iframe can fail. If you rename it to counter.htm (make sure to also change wherever it's referenced in the other code, so it gets updated), then there should be no problem.Code:<iframe name="counter" src="path/to/counter.txt" width="90" height="30" scrolling="no" frameborder="0"></iframe>
Using AJAX for this is complicated, and really a bit of overkill for such a simple piece of information unless you already have jQuery on the page, in which case it's fairly simple to load the contents of the counter file into a div element on the page. Again, it would be best if it were a .php or .htm file, but probably less critical than with iframe.
Depending on what your web host allows, you might be able to use .htaccess to parse PHP in HTML pages. Try these http://deano.me/2015/12/php-inside-h...sing-htaccess/
Hope that helps
Isn't it generally a bad idea to parse all HTML as PHP? Here's another bad idea (though it can be fine if you don't use the highlighted update feature too much):
If you don't need it updated, remove the highlighted. Currently it's set to update every 3 minutes, which is reasonable in most cases.Code:<!DOCTYPE html>
<html>
<head>
<title>Simple AJAX Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
#downloadcount {font: normal 95% verdana, arial, sans-serif;}
</style>
</head>
<body>
<div id="downloadcount">Number Downloaded: <span id="result"></span></div>
<script>
(function(url){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
document.getElementById('result').innerHTML = this.responseText;
}
};
xhttp.open('get', url + '?bust=' + new Date().getTime(), true);
xhttp.send();
setInterval(function(){
xhttp.open('get', url + '?bust=' + new Date().getTime(), true);
xhttp.send();
}, 3 * 60 * 1000);
})('counter.txt'); // use full path to counter.txt
</script>
</body>
</html>
I wouldn't say it's generally a bad idea to parse PHP in HTML. It makes the server work a little harder, yes, and it may create a bit of confusion with the organisation of files if somebody is expecting to see HTML in an .html file and PHP in a .php file (although with internal CSS and JavaScript already in HTML, and HTML in PHP, what's a little PHP in HTML going to do to the confusion level?), so I suppose the answer would be "it depends". To be honest, most of the web hosts and server environments I've worked on in the last 10 years had been setup to parse PHP in HTML files by default and I haven't had to fiddle with .htaccess at all. Maybe that's because they're private servers, semi-dedicated servers and "pro" level accounts on shared hosting though? It could be that more basic level shared hosting doesn't allow it.
Dear Beverleyh,
I tried with step 4. i tried edit the value 0 to 55 or random number, it was working fine n showed the result.
But when i click the link it redirect to file link and opens it but doesn't increase the count by 1.
Counting is not done, i should say.
What to do?
I have tried this the download works but the counter display does not show no web page
got them both working now, Thanks
Now If I could just get help with one more thing...
How do i gag my passphrase from the logs? Zmud was doing it automatically, Cmud is not.
Counter is not updating for me.
PS: I'm using Cloudflare on the site
I have registered just to say thank you Beverley!
I'm self-taught, I know - doesn't inspire confidence ;-)
and new to html/css & javascript (but from a C64 generation and with some success using C#). PHP scares me a little.
I found your second post, well commented code, extremely useful, inspiring... and with tinkering it works!
I had to rename my .html page as .php (my host?) and keep the html download on the page (or the .php didn't return to the download page).
I will explore why ... but supplying a simple useable code for counting downloads has been very kind - thank you again!
If you want to track lots of downloads though, you'd be better off using a download/click tracker with a database