• Hi,

    I want to have my music available on my blog. Right now I have a link to one mp3 file on my sidebar. For me to see how many times the link has been clicked I’ve added a click counter to that link. I noticed that it’s possible to have visible click counter on the site for example next to that link if I just add tiny script there. I just can’t figure out how to add that script next to link.

    I will be adding more mp3 files to my site in future and I just want to have links which look like this:

    Song 1 [10 downloads]
    Song 2 [23 downloads]
    Song 3 [15 downloads]

    Inside the [] are the tiny scripts which count the clicks of those links.

    Is there any way to add for example some script or HTML or some other code next to links? I tried to search some plugins which would make this possible but I couldn’t find that kind of plugin.

    Thanks for you help.

    – Tuomas

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter blogthomasmedianet

    (@blogthomasmedianet)

    Anyone?

    Thread Starter blogthomasmedianet

    (@blogthomasmedianet)

    I assume that it’s not possible then.

    You’ll need more than a tiny script.

    When someone clicks a link to download a song, you’ll need a way to record that download – and persist it to either a flat file or a database to keep count. Then, you’ll need another script to get a total for the display between the brackets.

    Example:

    Cog in the Wheel [329 downlaods]

    ‘Cog in the Wheel’ needs to be a link that passes a variable (to identify which song is being downloaded) to a script something like this:

    <?php
    $filename = ($_GET[song]);
    $dlfile = "/home/username/cgi-bin/dl-count/".$filename;
    header("Content-Disposition: attachment; filename=".$filename);
    header("Content-Length: ".filesize($dlfile));
    readfile($dlfile);
    $counter_file = "/home/username/cgi-bin/dl-count/downloads.txt";
    $filelock = "/home/username/cgi-bin/dl-count/file-lock.txt";
    $got_it = false;
    $timeout = 10;
    $fp = fopen($counter_file,"a");
    $timestamp = time();
    $hourdiff = "0"; //0 = same time zone
    $timeadjust = ($hourdiff * 60 * 60);
    while (($got_it === false) && ($timeout != 0))
    {
    if (flock($fp, LOCK_EX))
    {
    fwrite($fp, $timestamp."|".$filename."n");
    flock($fp, LOCK_UN);
    $got_it = true;
    }
    else
    {
    usleep(100000);
    --$timeout;
    }
    }
    if (!$got_it)
    {
    $fplock=fopen($filelock, 'a');
    fwrite($fplock,date("M d Y h:i a",time() + $timeadjust)."|".$counter_file."n");
    fclose($fplock);
    fclose($fp);
    exit;
    }
    fclose($fp);
    ?>

    The above script records each download, in the form of a timestamp, to a file.

    If you use this, then you’ll need another script that counts the timestamps for each song and puts the totals in variables that can be used like this:

    Cog in the Wheel [ <$php echo $dl-cog; ?> downloads ]

    You could do this in a loop if you have several songs.

    I haven’t provided an example of the second script because my beer is getting warm. But, yes, it is possible.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘How to add code/html/whatever next to links?’ is closed to new replies.