#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0

    Need help with click tracker


    i need a script that allows me to display how many times a file is downloaded
    please understand that i want the ability to display that on my site...giving me a script that just counts that on places it in a counter.php file or in a control panel is NO good for me

    i want it like this:
    sxc.hu/category/9001
    see how it lists under each image how many times its been downloaded

    i couldnt find any script that gives me that ability yet. but hundreds of sites use that.

    any ideas??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    76
    when the download link is clicked update the database using $counter += 1; and use php to make an image and include in the image how many times the thing has been downloaded from the database

    go to the link below to learn how to make images and use the images in php
    Php image tutorial

    Comments on this post

    • Triffic agrees : Cool never knew PHP could create images like that tutorial you linked to :)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0
    ok can you be more specific please
    where exactly should i add that line?

    do you mean it will list how many times the image has been downloaded as text ?
    please elaborate a little more
    thankx
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,794
    Rep Power
    529
    Displaying the number of times is easy.

    PHP Code:
    $sql ='SELECT counter FROM download_table';
    $stmt db::db()->query($sql);
    $counter $stmt->fetchColumn();
    echo(
    "sxc.hu/category/{$counter}"); 
    Keeping track of the number of downloads is more tricky. Just allowing them to download a file using Apache will not allow you to keep track of them. Instead, you will need to use PHP.

    PHP Code:
    //First, see if the file exists
    if (!is_file($file)) { die("<b>404 File not found!</b>"); }

    $sql ='UPDATE download_table SET counter=counter+1';
    $stmt db::db()->exec($sql);

    $fileInfo=library::getFileInfo(library::getExt($file_name));

    //Begin writing headers
    header('Pragma: public');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: public');
    header('Content-Description: File Transfer');

    //Use the switch-generated Content-Type
    header('Content-Type: '.$mimi);

    //Force the download
    header('Content-Disposition: attachment; filename='.$file_name.';' );
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($file));
    @
    readfile($file); 
    Last edited by NotionCommotion; April 5th, 2013 at 09:15 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0
    NotionCommotion:
    can you please tell me exactly where i should place those 2 codes?

    this my config.php file:

    <?php

    # config.php
    # SET VARIABLES #################################

    # Change your password. Very important!

    $password = "";

    # Set the full path to this directory with trailing slash /.

    $full_path = "C:";

    # Set your mysql database information.

    $db_name = "";
    $db_user = "";
    $db_host = "";
    $db_password = "";

    #
    # END SETTING VARIABLES #################

    $mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);

    if(mysqli_connect_errno())
    {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
    }

    and this is my go.php page that the link is sent through to track:

    <?php

    # go.php

    include_once("config.php");

    $page = (isset($_SERVER["QUERY_STRING"])) ? urldecode($_SERVER["QUERY_STRING"]):NULL;

    if($stmt = $mysqli->prepare("INSERT INTO clicktracker (xpage) VALUES (?)"))
    {
    $stmt->bind_param("s",$page);
    $stmt->execute();
    $stmt->close();
    }

    $mysqli->close();

    header("Location: $page");
    exit();

    ?>


    so where exactly should i add those 2 codes you wrote?

    and what do i have to add to the link on html for it display that number?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,794
    Rep Power
    529
    jfiler20,

    Your original post said you wanted to track how many times a "file" was downloaded. I assumed you mean some file like somefile.jpg. As such, you need to somehow keep track of this, and that is why I gave you the second section of code.

    If you want to track how many times your website is viewed (i.e. go.php is downloaded), then your job just got easier. Assuming this is the case, you would just need to put db::db()->exec('UPDATE download_table SET counter=counter+1'); in go.php.

    As for as displaying the number of counts, it would be in the section that displays your page's content.

    No changes are needed in config.php.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0
    ok so my go.php would be like this:

    <?php

    # go.php

    include_once("config.php");

    $page = (isset($_SERVER["QUERY_STRING"])) ? urldecode($_SERVER["QUERY_STRING"]):NULL;

    if($stmt = $mysqli->prepare("INSERT INTO clicktracker (xpage) VALUES (?)"))
    {
    $stmt->bind_param("s",$page);
    $stmt->execute('UPDATE download_table SET counter=counter+1');
    $stmt->close();
    }

    $mysqli->close();

    header("Location: $page");
    exit();

    ?>

    is this correct?

    and whats the code i have to place in html for it to display the counter?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,794
    Rep Power
    529
    PHP Code:
    //Create a database connection
    include_once("config.php");

    //Get the query string, if any, via which the page was accessed. 
    $page = (isset($_SERVER["QUERY_STRING"])) ? urldecode($_SERVER["QUERY_STRING"]):NULL;

    /* Update tables "clicktracker" of your database.
    I might have mislead you regarding using table "download_table".
    Maybe create a table who's PK represents each page.
    Then update the value just for that row.
    Also, use INSERT... ON DUPLICATE KEY UPDATE so you add a new row if that page has never been clicked.
    Also, I don't use mysqli, but I very much believe you don't have this quite right.
    */
    if($stmt $mysqli->prepare("INSERT INTO clicktracker (xpage) VALUES (?)"))
    {
    $stmt->bind_param("s",$page);
    $stmt->execute('UPDATE download_table SET counter=counter+1');
    $stmt->close();
    }

    $mysqli->close();

    /* What are you doing here?
    What is displayed
    Why go to another page, and not just display your page content directly here?
    Get this fixed before you do anything else?
    */
    header("Location: $page");
    exit(); 
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0
    ok ill try to get that fixed

    but please whats the html code i have insert so that it displays that number on my wesbite?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,794
    Rep Power
    529
    PHP Code:
     $sql ='SELECT counter FROM download_table';
    $stmt db::db()->query($sql);
    $counter $stmt->fetchColumn();
    echo(
    "<h1>sxc.hu/category/{$counter}</h1>"); 
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0
    that script just wasnt working right and i cant figure out why

    so i just searched for another script and found another one on github and it seems better

    it counts correctly how many times downloaded, but when i want to display it on the html it doesnt work right

    this is display.php:

    PHP Code:
    <?php

    if (!file_exists("config.php")) {
        
    header("Location: installer");
    }

    require_once(
    "config.php");

    //Connect to database
    @$con mysql_connect(DB_HOSTDB_USERDB_PASSWORD);
    if (!
    $con) {
        die(
    "Error: Could not connect to database (" mysql_error() . "). Check your database settings are correct.");
    }

    //Check database exists
    $does_db_exist mysql_select_db(DB_NAME$con);
    if (!
    $does_db_exist) {
        die(
    "Error: Database does not exist (" mysql_error() . "). Check your database settings are correct.");
    }

    if (isset(
    $_GET["id"])) {
        
    $id mysql_real_escape_string($_GET["id"]);
    } else {
        die(
    "Error: ID cannot be blank.");
    }

    //If ID exists, show count or else die
    $showinfo mysql_query("SELECT count FROM Data WHERE id = \"$id\"");
    $showresult mysql_fetch_assoc($showinfo);
    if (
    $showresult != 0) {
        echo 
    $showresult["count"];
    } else {
        die(
    "Error: ID does not exist.");
    }

    mysql_close($con);

    ?> ?>
    and then it says to insert htis code in html to show how many times downloaded:

    PHP Code:
    <?php
    $_GET
    ["id"] = "download1";
    include(
    "indication/display.php");
    ?>
    I placed that code in my html but it doesnt show the number

IMN logo majestic logo threadwatch logo seochat tools logo