Created
July 2, 2020 13:04
-
-
Save IslandOfCode/b3dfe4611643c4e7bcad45a3e0485c6f to your computer and use it in GitHub Desktop.
Simple download counter in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RewriteEngine On | |
RedirectMatch ^.*\.(zip|rar|pdf)$ /download.php?file=$0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Creato da/Made by Pier Riccardo Monzo, IslandOfCode.it | |
* L'uso è libero, una citazione è apprezzata. | |
* Free to use, a mention is appreciated. | |
*/ | |
/***** FUNZIONI DI SUPPORTO */ | |
function getRealIPAddr(){ | |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { | |
$ip=$_SERVER['HTTP_CLIENT_IP']; | |
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ | |
$ip=$_SERVER['HTTP_X_FORWARDED_FOR']; | |
}else{ | |
$ip=$_SERVER['REMOTE_ADDR']; | |
} | |
return $ip; | |
} | |
/***** IMPOSTAZIONI E VARIABILI */ | |
date_default_timezone_set("Europe/Rome"); | |
$DB_FILE_COUNTER = 'counter.sqlite'; | |
$path = '/web/htdocs/www.example.it/home'; | |
/***** CONTROLLO PARAMETRI GET */ | |
if(!array_key_exists("file",$_GET)){ | |
die('FILE NON SPECIFICATO!'); | |
} | |
/***** RECUPERO E RIELABORAZIONE PARAMETRO GET */ | |
$baseFileName = $_GET['file']; | |
$file = $path.$_GET['file']; | |
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\\')); | |
/***** INIT DATABASE E VARIABILI VARIE */ | |
$db = new SQLite3($DB_FILE_COUNTER, SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE); | |
$remoteIP = getRealIPAddr(); | |
$today = date("Y-m-d", time()); | |
/***** RICERCA POSSIBILE ENTRY PRE-ESISTENTE */ | |
$stm = $db->prepare('SELECT * FROM DOWNCOUNTER WHERE IP = ? AND DATE = ? AND FILENAME = ?'); | |
$stm->bindValue(1,$remoteIP); | |
$stm->bindValue(2,$today); | |
$stm->bindValue(3,$baseFileName); | |
$rs = $stm->execute(); | |
$frs = $rs->fetchArray(SQLITE3_ASSOC); | |
// Se questa combinazione di IP/DATA/NOME FILE esiste, bisogna solo aggiornare il numero di download | |
if(!empty($frs)){ | |
$stm = $db->prepare('UPDATE DOWNCOUNTER SET HIT = ? WHERE IP = ? AND DATE = ? AND FILENAME = ?'); | |
$stm->bindValue(1,intval($frs['HIT'])+1); | |
$stm->bindValue(2,$remoteIP); | |
$stm->bindValue(3,$today); | |
$stm->bindValue(4,$baseFileName); | |
$stm->execute(); | |
} else { | |
//Questo IP sta scaricando questo FILE per la prima volta OGGI, inserisci nuova ENTRY nella tabella | |
$stm = $db->prepare('INSERT INTO DOWNCOUNTER(IP,DATE,FILENAME) VALUES(?,?,?)'); | |
$stm->bindValue(1,$remoteIP); | |
$stm->bindValue(2,$today); | |
$stm->bindValue(3,$baseFileName); | |
//HIT è 1 di default | |
$stm->execute(); | |
} | |
/***** CREAZIONE HEADER PER LA RISPOSTA */ | |
$size = filesize($file); | |
header('Content-Description: File Transfer'); | |
header('Content-Type: application/octet-stream'); | |
header('Content-Disposition: attachment; filename=' . $quoted); | |
header('Content-Transfer-Encoding: binary'); | |
header('Connection: Keep-Alive'); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); | |
header('Pragma: public'); | |
header('Content-Length: ' . $size); | |
/***** LETTURA FILE PER IL DOWNLOAD */ | |
ob_clean(); | |
flush(); | |
readfile($file); | |
exit; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE "DOWNCOUNTER" ( | |
"IP" TEXT NOT NULL, | |
"DATE" TEXT NOT NULL, | |
"FILENAME" TEXT NOT NULL, | |
"HIT" INTEGER NOT NULL DEFAULT 1, | |
PRIMARY KEY("IP","DATE","FILENAME") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment