/    Sign up×
Community /Pin to ProfileBookmark

Download Counter

I’ve gone through about 10 different php & javascript codes for a Download Counter for my new Fonts Section i want to put on my site. I want to track the number of downloads for each font’s zip file…I have mysql, server, ftp access…Full Access. About PERL, Im not sure what that even is…

I was close at one point but I don’t know what I did wrong..If someone can hook me up, I would be greatly appreciated.

It’s really important. Please&Thanks 😮

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@pcthugAug 01.2006 — [code=php]
<?php
// location of zip file
$filename = 'path/to/zip/file.zip';

function increment_download_count() {
mysql_query('UPDATE table_name SET count = count + 1');
}

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);

increment_download_count()
exit();

?>
[/code]
Copy linkTweet thisAlerts:
@Andrew2authorAug 01.2006 — [code=php]
<?php
// location of zip file
$filename = 'path/to/zip/file.zip';

function increment_download_count() {
mysql_query('UPDATE table_name SET count = count + 1');
}

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);

increment_download_count()
exit();

?>
[/code]
[/QUOTE]


Do you have any instructions on setup? I am not that great with PHP...
Copy linkTweet thisAlerts:
@pcthugAug 02.2006 — Well providing you have a php enabled server you would:
[LIST=1]
  • [*]Edit line 3 of the previously posted script to match the location of your .zip file:
    [code=php]$filename = 'You would edit this to the path of the .zip file';[/code]

  • [*]Save the file as say; [I]download.php[/I]

  • [*]Upload the file to your server

  • [*]Create a new db table and amend [B]table_name[/B] of line 6 (see below) of the script to match.
    [code=php] mysql_query('UPDATE table_name SET count = count + 1');[/code]

  • [*]Create a new [B]SMALLINT[/B] column within this table named [B]count[/B]

  • [*]If you have not yet established a mysql connection and db selection be sure to include the following to the top of the script
    [code=php]
    // you must get your db_username, db_password and db_name from your host
    mysql_connect('localhost', 'db_username', 'db_password');
    mysql_select_db('db_name');
    [/code]

  • [/LIST]

    Finally you would visit download.php in your browser where simultaneously you would be prompted to download the .zip file specified in step 1, as well as having your download counted.
    Copy linkTweet thisAlerts:
    @WebMaisterMay 28.2008 — Great code!

    FYI, this could be a variant that make no need of MySQL

    [code=php]<?php

    $download_file = "file.zip"; //location of download file (the counter file will be created in the same location)

    function IncrementDownloadCounter($counter_file)
    {
    if( !($fh = fopen($counter_file, "r+b")) )
    die();

    $count = (int)fread($fh, 10); //reads 10 bytes we are supposing max 9 billions counts (9999999999)

    rewind($fh);
    if(!fwrite($fh, ++$count))
    die();

    fclose($fh);

    return TRUE;
    }

    if(file_exists($download_file))
    {
    if(!IncrementDownloadCounter($download_file . ".txt"))
    die();

    /*
    Sources:
    http://www.opendesigns.org/forum/discussion/1437/php-download-counter/#pgbottom
    http://www.webdeveloper.com/forum/showthread.php?t=115815&highlight=PHP+download+counter
    http://it2.php.net/manual/en/function.header.php#83384
    */

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: application/zip");
    header('Content-Disposition: attachment; filename="' . basename($download_file) . '"'); //ok
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . filesize($download_file)); //ok
    readfile($filename);

    }
    else
    {
    print "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">";
    print "<html>";
    print "<head>";
    print "<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">";
    print "<title>Download file unavailable</title>";
    print "</head>";
    print "<body>";
    print "<h1>Sorry, file '" . $download_file . "' is temporary unavailable.<br>Please retry later.</h1>";
    print "</body>";

    }

    ?>[/code]




    Besides the code [B]if anyone better understand the headers sent by PHP code, could he explain them to me?![/B]

    Thanks! :o
    ×

    Success!

    Help @Andrew2 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...