Link click counter

Discussion in 'PHP' started by emi87, Oct 11, 2008.

  1. #1
    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!
     
    emi87, Oct 11, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    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 :)
     
    EricBruggema, Oct 11, 2008 IP
  3. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Could you please post in details.. about this option?
    Also if possible, please try to post the code too... that will be very grrreat...;););)
     
    badmasketa, Oct 12, 2008 IP
  4. songsmastee

    songsmastee Peon

    Messages:
    87
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hmm first option is good.
     
    songsmastee, Oct 12, 2008 IP
  5. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #5
    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):
     
    djzmo, Oct 12, 2008 IP
  6. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thank you djzmo is working very well :D :D, 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
     
    emi87, Oct 13, 2008 IP
  7. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    no one knows?? :( :(
     
    emi87, Oct 15, 2008 IP
  8. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #8
    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']??
     
    lp1051, Oct 15, 2008 IP
  9. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #9
    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
     
    ads2help, Oct 15, 2008 IP
  10. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #10
    First option is simple and easy to implement, but might reset your counter in case of multiple accesses at the same time.
     
    webrickco, Oct 15, 2008 IP