Counting clicks

Discussion in 'Web Design & Coding' started by DFX, Mar 22, 2003.

  1. DFX

    DFX OSNN Senior Addict

    Messages:
    342
    Location:
    England
    Hi,

    Im making a website so I can host my own DJ mixes. I was wondering if there was a counter that counts how many clicks so I can display it on the page.

    So it looks like this:

    March Mix
    Listen [amount of users listened: 28]

    Anyone got any ideas?
     
  2. Tabula Rasa

    Tabula Rasa Stranger Than Kindness Political User

    Messages:
    3,233
    Location:
    Israel
    try this:
    http://www.spoono.com/tutorials/php/tracker/

    youl need to alter this a bit to suite your goal (call out the numbers of clicks from the mysql database to be displayed to the public and such)

    maybe someone else will offer something more specific for your goal.
     
  3. DFX

    DFX OSNN Senior Addict

    Messages:
    342
    Location:
    England
    MySQL isn't an option Im afraid. Is there anyway I can use a .txt file or something similar?
     
  4. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    Your page
    <a href="dlmix.php?download=mix.mp3">mix.mp3</a> downloaded <?include("mix.txt");?> times

    dlmix.php
    <?php
    if (file_exists("mix.txt")) {
    $file = fopen("mix.txt", "r+");
    flock($file, 1);
    $count = fgets($file, 4096);
    $count += 1;
    fseek($file,0);
    fputs($file,$count);
    flock($file, 3);
    fclose($file);
    }
    header("location: $download");
    ?>


    I think that will work, I haven't tried it tho..
     
  5. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    Ok, changed it about a bit, added a function to make it easier

    The Page
    Code:
    <?php 
    function download($download) { print "
    <a href=\"download.php?download=$download\">Listen</a><br><b> ";
    include("$download.txt");
    print "</b> people listened so far";
    }
    ?>
    //content
    March Mix - <? download("audio/march.mp3"); ?>
    <br>
    April Mix - <? download("audio/april.mp3"); ?>
    
    //content
    
    download.php
    Code:
    <?php
    if (file_exists("$download.txt")) {
    $file = fopen("$download.txt", "r+");
    flock($file, 1);
    $count = fgets($file, 4096);
    $count += 1;
    fseek($file,0);
    fputs($file,$count);
    flock($file, 3);
    fclose($file);
    }
    else print "Invalid download specified";
    break;
    header("location: $download");
    ?>
    Now all you need is to aim the download(); function at a file, and have another file in the same directory with the same name and the addition of a .txt extension :)
    example :
    download.php?download=audio/blah.mp3
    you will require audio/blah.mp3.txt to exist.