<?php
######################################################################
######################################################################
### ###
### Download Counter Version 0.95 ###
### Copyright 2004 Eljee Bergwerff php@eljee.com ###
### Created 10-2004 Last Modified Today ###
### Scripts Name: download_counter.php ###
### ###
######################################################################
######################################################################
//This is a basic DOWNLOAD COUNTER script created with simplicity in mind.
//
//features: -Add a link behind the script and it will start counting that link directly. No additional setup
// -Overview table with links and download counts
// -Easy removal and resetting of download counters
// -Very easy setup, just change some variables and off you go
// -One single file with setup function
// -Counts as much files as you want
// -Downloaders will not notice anything
//
//instructions: -First edit the MySQL data below to reflect your mysql settings.
// -Run http://www.yourdomain.com/download_counter.php?file=setup
// -done!
//
//usage: -add a link behind the 'file=' like this:
// http://www.yourdomain.com/download_counter.php?file=http://www.foo.bar/program.zip
// -A new record for http://www.foo.bar/program.zip will be created automaticly and counting will begin
// -go to http://www.yourdomain.com/download_counter.php to view the counters!
// MySQL data
$db_host = "localhost"; //Mysql Host
$db_login = "kemas"; //Mysql username
$db_pass = "kemas1980"; //Mysql password
$db_name = "slampp"; //Mysql database This can be a existing database.
//If so, take a look at the Table Name $db_table_files to make sure it doesn't conflict with a existing table
//If the database doesn't exist the script wil try to create one
//Webmaster Email
$webmaster_email = "kyantonius@kyantonius.com";
//Table and Fields data
//There is no need to change this, just in case you want to
$db_table_files = "files_counter";
$db_files_id = "id";
$db_files_link_url = "link_url";
$db_files_link_total = "link_total";
$db_files_link_date = "link_date";
//In case anything went wrong with the setup of the tables you can create them manually also:
//
// CREATE DATABASE `download_counter`;
// USE download_counter;
//
// CREATE TABLE `files` (
// `id` int(5) NOT NULL auto_increment,
// `link_url` varchar(255) NOT NULL default '',
// `link_total` varchar(10) NOT NULL default '0',
// `link_date` datetime NOT NULL default '0000-00-00 00:00:00',
// PRIMARY KEY (`id`),
// UNIQUE KEY `link_url` (`link_url`)
// ) TYPE=MyISAM AUTO_INCREMENT=1;
####################################################################################
//do not change anything below this line (if you don't need to..)
#####################
#Open DB
#####################
$db=0;
openDB();
#####################
#dertimine what to do
#####################
if ($file == '') {showlist();exit;}
if ($file == 'list') {showlist();exit;}
if ($file == 'delete') {delete($_GET[id]);showlist();exit;}
if ($file == 'reset_counter') {reset_counter($_GET[id]);showlist();exit;}
if ($file == 'setup') {setup();showlist();exit;}
#####################
#Check if this is a new link
#####################
$query = "SELECT * FROM `$db_table_files` WHERE `$db_files_link_url` = '$file'";
if (!
($output)) {addnewlink
();header
("Location: http://$_SERVER[SERVER_NAME]$_SERVER[SCRIPT_NAME]?file=$file");
exit;
}
else {addcouter($output[$db_files_id],$output[$db_files_link_total]);
else{echo"Error: Please email $webmaster_email";
}
}
#####################
#Functions
#####################
//ADD NEW LINK after has been checked if the link in the URL is already present in the database a new record will be added.
//This new record will contain the link, the add date and the amount of downloads
function addnewlink(){
global $db_table_files,
$db_files_link_url,
$file,
$db_files_link_date;
$insertorupdate = "insert";
$now =
date( "Y-m-d H-i-s");
$data =
array("$db_files_link_url"=>
"$file",
"$db_files_link_date"=>
"$now");
safe($insertorupdate,$db_table_files,$data,$id);
}
//AAD COUNTER will read the amount of downloads of a link, add one and safe it again to the database
function addcouter($id,$link_total){
global $db_table_files,
$db_files_link_total;
$link_total++;
$data =
array("$db_files_link_total"=>
"$link_total");
safe(update,$db_table_files,$data,$id);
}
//SAFE this is a insert or update function to make adding things to the database a bit more easy.
function safe($insertorupdate,$table,$data,$id){
if ($insertorupdate == "insert")
{
$insertorupdate = "insert into $table";
foreach($data as $k => $v) {
$keystring .="`$k`, ";
$valuestring .= "'$v', ";
}
$valuestring =
substr("$valuestring",
0,
-2);
$keystring =
substr("$keystring",
0,
-2);
$data = "($keystring) VALUES ($valuestring)";
}
else
{
$insertorupdate = "update $table set";
foreach($data as $k => $v) {
$datastring .="$k = \"$v\", ";
}
$datastring =
substr("$datastring",
0,
-2);
$data = $datastring;
}
if ($id == ''){$where = "";}
else{$where = "where id = '$id'";}
$query="$insertorupdate $data $where";
if ($result){
$succes = 1;
}
else{
echo "<br>Email: $webmaster_email";
}
return;
}
//OPEN DB a function to make the connection to the database
function openDB()
{ global $db,
$db_host,
$db_login,
$db_pass,
$db_name;
}
//SHOWLIST reads all links in the database and print them in a table with links to reset or delete the link.
//It will also show the amount of downloads
function showlist()
{
$db_files_link_url,
$db_files_link_total,
$db_files_link_date,
$db_files_id;
$result=
mysql_query("SELECT * FROM `$db_table_files`");
echo "<table border=1 width=100% cellspacing=0 bordercolor=#C5C5C5><tr>";
echo "<b>Download Counter</b><br> <td><b>[Url]</b></td><td><b>[Downloads]</b></td></tr>\r\n";
{
$url_array =
split('[/]',
$row[$db_files_link_url]);
list($db_files_link_file) =
$url_array;
echo "<td><a href=\"http://$_SERVER[SERVER_NAME]$_SERVER[SCRIPT_NAME]?file=$row[$db_files_link_url]\">$row[$db_files_link_url]</a></td>\r\n";
if ($row[$db_files_link_total] == 1){$s="";}else{$s="s";}
echo "<td>$row[$db_files_link_total] time$s since $row[$db_files_link_date]</td>\r\n";
}
}
?>