Hello, <a href="music/track01.mp3">ATB - The autumn leaves</a> I have a link to a .mp3 file I want to count how many times the .mp3 file has been downloaded. Please not with click counter like this http://www.phpjunkyard.com/php-click-counter.php I need a simple script. Thank you!
Its easy to create a script for that purpose. 2 options 1. Using filesystem - Create a script that will use the argument ($_GET) for file input - Check if the file exists - Add a filename.counter file to the directory if it doen't exists and add 1 into the file - If filename.counter exists then read content and add one to the current number 2. Using MySQL - Create a script using $_GET - Check if file exists in database - Read hits from database - Check if file exists on server - Yes, add one hit and send headers to start download for user - No, no hit and display error page. Hope you understand it all
Could you please post in details.. about this option? Also if possible, please try to post the code too... that will be very grrreat...
Let's say that you have this link <a href="music/track01.mp3">ATB - The autumn leaves</a> Code (markup): now edit the value of href to a PHP file that will record the counter data. for example, call it add_hit.php. so the link will be like this <a href="add_hit.php">ATB - The autumn leaves</a> Code (markup): Now create a PHP file called add_hit.php, and use these codes inside the PHP file. <?php $song_location = "music/track01.mp3"; $file = fopen("songcounter.txt", 'w') or die(); fwrite($file, '.'); fclose($file); header("Location: $song_location"); ?> PHP: Then create a blank text file called songcounter.txt Now every click to your song will be counted. To retrieve the data, simply use the code below. This file has been downloaded for <?=strlen(@file_get_contents('songcounter.txt'))?> times Code (markup):
Thank you djzmo is working very well , but I have a little problem My link was like this: <a href="music/'.$row['song'].'">'.$row['song'].'</a> Now after I have changed as you told me I must to pass a variable to the add_hit.php because is needed for variable $song_location = "music/'.$row['song'].'"; how can i make this variable pass in my example ..I have read some examples on google but no luck thank you
what about just : <a href="add_hit.php?song_location=music/".$row['song']."">".$row['song']."</a> and read it in add_hit.php as a $_GET['song_location']??
edited from djzmo's at your music page <a href="add_hit.php?song='.$row['song'].'">'.$row['song'].'</a> PHP: at your add_hit.php <?php $song_location = $_GET['song']; $file = fopen("songcounter.txt", 'w') or die(); fwrite($file, '.'); fclose($file); header("Location: $song_location"); ?> PHP: this is insecure
First option is simple and easy to implement, but might reset your counter in case of multiple accesses at the same time.